Alvin Ofori
Lurker
I am creating a messaging app and I need to show heads-up notifications on Android 7 and older up to Android 5.
I have created FirebaseMessagingService together with NotificationChannel to show notifications for Android 8 and higher and it works well with heads-up notifications. On Android 7 FirebaseMessagingService onMessageReceived method doesn't work when the app is in the background. So I've decided to use the BroadcastReceiver to show heads-up notifications and now the heads-up notification is shown on Android 7 for several seconds and then it stays in the drawer together with a normal notification. I even commented out FirebaseMessagingService but still get the ordinary notification together with the heads-up notification. I have a feeling that there is another way of implementing this.
Here is my code:
MyFirebaseMessagingService file:
Calling the showNotification() from FirebaseBackgroundService file:
My JSON looks like this:
I have created FirebaseMessagingService together with NotificationChannel to show notifications for Android 8 and higher and it works well with heads-up notifications. On Android 7 FirebaseMessagingService onMessageReceived method doesn't work when the app is in the background. So I've decided to use the BroadcastReceiver to show heads-up notifications and now the heads-up notification is shown on Android 7 for several seconds and then it stays in the drawer together with a normal notification. I even commented out FirebaseMessagingService but still get the ordinary notification together with the heads-up notification. I have a feeling that there is another way of implementing this.
Here is my code:
MyFirebaseMessagingService file:
Java:
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
// if (remoteMessage.notification !=null) {
// showNotification(remoteMessage.notification?.title, remoteMessage.notification?.body)
// }
}
fun showNotification(title: String?, body: String?, context: Context) {
val intent = Intent(context, SearchActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT)
val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(context, "my_channel_id_01").apply {
setSmallIcon(R.drawable.my_friends_room_logo)
setContentTitle(title)
setContentText(body)
setSound(soundUri)
setDefaults(DEFAULT_ALL)
setTimeoutAfter(2000)
setPriority(PRIORITY_HIGH)
setVibrate(LongArray(0))
}
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(0, notificationBuilder.build())
}
Calling the showNotification() from FirebaseBackgroundService file:
Java:
class FirebaseBackgroundService : BroadcastReceiver() {
var myFirebaseMessagingService = MyFirebaseMessagingService()
var notificationtitle: Any? = ""
var notificationbody: Any? = ""
override fun onReceive(context: Context, intent: Intent) {
if (intent.extras != null) {
for (key in intent.extras!!.keySet()) {
val value = intent.extras!!.get(key)
Log.e("FirebaseDataReceiver", "Key: $key Value: $value")
if (key.equals("gcm.notification.title", ignoreCase = true) && value != null) {
notificationtitle = value
}
if (key.equals("gcm.notification.body", ignoreCase = true) && value != null) {
notificationbody = value
}
}
myFirebaseMessagingService.showNotification(notificationtitle as String, notificationbody as String, context)
}
}
}
My JSON looks like this:
Code:
{
"to" : "some-id",
"priority":"high",
"notification" : {
"body" : "Body of Your Notification",
"title": "Title of Your Notification",
"content_available" : true,
"sound": "sound1.mp3",
"click_action" : "chat"
},
"data": {
"uid" : "yOMX4OagvgXEl4w4l78F7SlqzKr2",
"method" : "chat",
"android_channel_id": "1"
}
}