|
| 1 | +package com.httpsms |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.util.Log |
| 5 | +import androidx.work.OneTimeWorkRequest |
| 6 | +import androidx.work.WorkManager |
| 7 | +import androidx.work.Worker |
| 8 | +import androidx.work.WorkerParameters |
| 9 | +import com.google.firebase.messaging.FirebaseMessagingService |
| 10 | +import com.google.firebase.messaging.RemoteMessage |
| 11 | + |
| 12 | +class MyFirebaseMessagingService : FirebaseMessagingService() { |
| 13 | + |
| 14 | + // [START receive_message] |
| 15 | + override fun onMessageReceived(remoteMessage: RemoteMessage) { |
| 16 | + // TODO(developer): Handle FCM messages here. |
| 17 | + // Not getting messages here? See why this may be: https://goo.gl/39bRNJ |
| 18 | + Log.d(TAG, "From: ${remoteMessage.from}") |
| 19 | + |
| 20 | + scheduleJob() |
| 21 | + |
| 22 | + // Check if message contains a notification payload. |
| 23 | + remoteMessage.notification?.let { |
| 24 | + Log.d(TAG, "Message Notification Body: ${it.body}") |
| 25 | + } |
| 26 | + |
| 27 | + // Also if you intend on generating your own notifications as a result of a received FCM |
| 28 | + // message, here is where that should be initiated. See sendNotification method below. |
| 29 | + } |
| 30 | + // [END receive_message] |
| 31 | + |
| 32 | + // [START on_new_token] |
| 33 | + /** |
| 34 | + * Called if the FCM registration token is updated. This may occur if the security of |
| 35 | + * the previous token had been compromised. Note that this is called when the |
| 36 | + * FCM registration token is initially generated so this is where you would retrieve the token. |
| 37 | + */ |
| 38 | + override fun onNewToken(token: String) { |
| 39 | + Log.d(TAG, "Refreshed token: $token") |
| 40 | + |
| 41 | + // If you want to send messages to this application instance or |
| 42 | + // manage this apps subscriptions on the server side, send the |
| 43 | + // FCM registration token to your app server. |
| 44 | + sendRegistrationToServer(token) |
| 45 | + } |
| 46 | + // [END on_new_token] |
| 47 | + |
| 48 | + private fun scheduleJob() { |
| 49 | + // [START dispatch_job] |
| 50 | + val work = OneTimeWorkRequest |
| 51 | + .Builder(SendSmsWorker::class.java) |
| 52 | + .build() |
| 53 | + |
| 54 | + WorkManager |
| 55 | + .getInstance(this) |
| 56 | + .enqueue(work) |
| 57 | + // [END dispatch_job] |
| 58 | + } |
| 59 | + private fun sendRegistrationToServer(token: String?) { |
| 60 | + // TODO: Implement this method to send token to your app server. |
| 61 | + Log.d(TAG, "sendRegistrationTokenToServer($token)") |
| 62 | + } |
| 63 | + |
| 64 | + companion object { |
| 65 | + private val TAG = MyFirebaseMessagingService::class.simpleName |
| 66 | + } |
| 67 | + |
| 68 | + internal class SendSmsWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) { |
| 69 | + override fun doWork(): Result { |
| 70 | + Log.i(TAG, "fetching messages") |
| 71 | + val smsService = SmsManagerService() |
| 72 | + val messages = HttpSmsApiService().getOutstandingMessages() |
| 73 | + messages.forEach { |
| 74 | + message -> smsService.SendMessage(this.applicationContext, message, null, null) |
| 75 | + } |
| 76 | + Log.i(TAG, "received [${messages.size}] messages") |
| 77 | + return Result.success() |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments