简体   繁体   中英

Android: FCM notification showing up incorrectly

I am sending notifications to specific device-tokens with Firebase Cloud Messaging. The notification is being sent from the backend in this form:

notification: {
    title: "New delivery request",
    body: order.id,
    sound: "default"
}

Initially, I was displaying the notification the exact way it was coming from FCM, therefore, the notification looked like this and I was quite comfortable with that:

在此处输入图像描述


I recently changed the implementation to the code below:

override fun onMessageReceived(remoteMessage: RemoteMessage) {

        var notificationTitle: String? = null
        var notificationBody: String? = null

        if (remoteMessage.notification != null) {
            notificationTitle = remoteMessage.notification!!.title
            notificationBody = remoteMessage.notification!!.body
        }

        val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

        val intent = Intent(this, HomeActivity::class.java)
        intent.putExtra("ORDER", notificationBody)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

        val pendingIntent = PendingIntent.getActivity(
            this,
            1234,
            intent,
            PendingIntent.FLAG_ONE_SHOT
        )

        val notificationBuilder =
            NotificationCompat.Builder(this, "REM")
                .setContentTitle(notificationTitle)
                .setContentText("Click to accept")
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setStyle(NotificationCompat.BigTextStyle())
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent)
                .setSmallIcon(android.R.drawable.sym_def_app_icon)
                .setAutoCancel(true)

        val notificationManager =
            ContextCompat.getSystemService(this, NotificationManager::class.java) as NotificationManager


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                "REM",
                "pushNotifications",
                NotificationManager.IMPORTANCE_DEFAULT
            )
            notificationManager.createNotificationChannel(channel)
        }

        notificationManager.notify(1234, notificationBuilder.build())

    }

From the code snippet above, what I expect to see now is a notification with the content-title set as the notificationTitle and content-text set as " Click to accept ". However, it's still showing the same way as the picture above. I have no idea why it's not changing. I have:

  • Invalidated cache and restarted.
  • Cleared app cache and data
  • Reinstalled the application

I'd really appreciate clarification on this issue. What can I do to make the notifications display the way I want?

The code is set up correctly.

Ideally, this is supposed to display Click to accept as the content-text of the notification. The reason why this is not happening is because of the way the backend is structured.

Here are two quick points to note:

  • When you send a notification payload to FCM, this basically means the notification would be displayed directly on the app without passing through onMessageReceived() . This is exactly what is happening in this case.

  • When you send a data payload, the notification is intercepted through your app's onMessageReceived() , here you can modify how you want the notification to be displayed.

Therefore, a quick fix to this issue would be to change notification to data on the backend and redeploy. Everything should work fine after that. Check out the snippet below:

data: {
    title: "New delivery request",
    body: order.id,
    sound: "default"
}

For more information on the FCM payload types, check this link out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM