如何在Kotlin上的Android 7及更早版本上显示抬头通知?

时间:2019-05-20 12:40:23

标签: android kotlin notifications android-7.0-nougat heads-up-notifications

我正在创建一个消息传递应用程序,我需要在Android 7和更早的Android 5上显示提示通知。

我已经与NotificationChannel一起创建了FirebaseMessagingService,以显示Android 8及更高版本的通知,并且它与平视通知非常兼容。在Android 7上,当应用程序处于后台时,FirebaseMessagingService的onMessageReceived方法不起作用。因此,我决定使用BroadcastReceiver来显示抬头通知,现在抬头通知会在Android 7上显示几秒钟,然后与常规通知一起留在抽屉中。我什至注释掉了FirebaseMessagingService,但仍然收到普通通知和抬头通知。我感觉还有另一种实现方法。

screenshot of how the notification looks now

这是我的代码:

MyFirebaseMessagingService文件:

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())
    }

从FirebaseBackgroundService文件中调用showNotification():

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)
       }
    }
}

我的JSON看起来像这样:

{
 "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"
   }
}

0 个答案:

没有答案