Advertisement

Responsive Advertisement

Firebase Push Notification Message using work manager

  •  First of all add dependency
    implementation("androidx.work:work-runtime-ktx:$work_version:2.9.0")
  • Now make class of MyFirebaseMessagingService.kt
    import android.app.NotificationChannel
    import android.app.NotificationManager
    import android.app.PendingIntent
    import android.content.Context
    import android.content.Intent
    import android.media.RingtoneManager
    import android.os.Build
    import android.util.Log
    import androidx.core.app.NotificationCompat
    import androidx.work.OneTimeWorkRequest
    import androidx.work.WorkManager
    import androidx.work.Worker
    import androidx.work.WorkerParameters
    import com.example.newnotify.MainActivity
    import com.example.newnotify.R
    import com.google.firebase.messaging.FirebaseMessagingService
    import com.google.firebase.messaging.RemoteMessage

    class MyFirebaseMessagingService : FirebaseMessagingService() {

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
    super.onMessageReceived(remoteMessage)

    if (remoteMessage.data.isNotEmpty()) {
    Log.d(TAG, "Message data payload: ${remoteMessage.data}")
    if (needsToBeScheduled()) {
    scheduleJob()
    } else {
    handleNow(remoteMessage)
    }
    }

    remoteMessage.notification?.let {
    Log.d(TAG, "Message Notification Body: ${it.body}")
    sendNotification(it.body ?: "")
    }
    }

    private fun needsToBeScheduled() = true

    override fun onNewToken(token: String) {
    Log.d(TAG, "Refreshed token: $token")
    sendRegistrationToServer(token)
    }

    private fun scheduleJob() {
    val work = OneTimeWorkRequest.Builder(MyWorker::class.java).build()
    WorkManager.getInstance(this).enqueue(work)
    }

    private fun handleNow(remoteMessage: RemoteMessage) {
    Log.d(TAG, "Short lived task is done.")
    sendNotification(remoteMessage.notification?.body ?: "")
    }

    private fun sendRegistrationToServer(token: String?) {
    Log.d(TAG, "sendRegistrationTokenToServer($token)")
    }

    private fun sendNotification(messageBody: String) {
    val intent = Intent(this, MainActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    val pendingIntent = PendingIntent.getActivity(
    this, 0, intent, PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
    )

    val channelId = "fcm_default_channel"
    val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
    val notificationBuilder = NotificationCompat.Builder(this, channelId)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentTitle("FCM Message")
    .setContentText(messageBody)
    .setAutoCancel(true)
    .setSound(defaultSoundUri)
    .setPriority(NotificationCompat.PRIORITY_HIGH)
    .setContentIntent(pendingIntent)

    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val channel = NotificationChannel(
    channelId,
    "Channel human readable title",
    NotificationManager.IMPORTANCE_DEFAULT
    )
    notificationManager.createNotificationChannel(channel)
    }

    val notificationId = 0
    notificationManager.notify(notificationId, notificationBuilder.build())
    }

    companion object {
    private const val TAG = "MyFirebaseMsgService"
    }

    internal class MyWorker(appContext: Context, workerParams: WorkerParameters) :
    Worker(appContext, workerParams) {
    override fun doWork(): Result {
    // Simulate long-running work here.
    return Result.success()
    }
    }
    }

  • In Manifest , make sure you have added permission and service
    
    
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <service
    android:name=".messaging.MyFirebaseMessagingService"
    android:exported="true">
    <intent-filter>
    <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
    </service>
  • Boom !! You'll get notifications.

Post a Comment

0 Comments