android:根据 FCM 通知的数据采取措施



我在使用Firebase Cloud Message接收通知的Android应用程序上工作,我将通知和数据从FCM发送到我的应用程序,我想在单击时根据FCM的数据进行操作在通知栏上,当我的应用在后台时,我使用下面的代码,当应用在前景中时正常工作,但是在后台,它只是打开应用而无需采取任何操作:

class MyFirebaseMessagingService : FirebaseMessagingService() {
    val TAG = "FirebaseMessagingService"
    var count = 0
    @SuppressLint("LongLogTag")
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        if (remoteMessage.data != null && remoteMessage.data.isNotEmpty()) {
            val id = remoteMessage.data["id"].toString()
            val activityName = remoteMessage.data["view"].toString()
            val intent = Intent(this, NewsDetailesActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            NewsDetailesActivity.currentNewId = id
            startActivity(intent)
            showNotification(remoteMessage.notification?.title, remoteMessage.notification?.body,activityName,id)
        }
    }
    private fun showNotification(title: String?, body: String?,activityName:String,id:String?) {
        var intent = Intent(this, NewsDetailesActivity::class.java)
        if (activityName == "news")
        {
            intent = Intent(this, NewsDetailesActivity::class.java)
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        NewsDetailesActivity.currentNewId = id!!
        val pendingIntent = PendingIntent.getActivity(
            this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT
        )
        val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val notificationBuilder = NotificationCompat.Builder(this, "channelId")
            .setSmallIcon(R.drawable.logo_orang)
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true)
            .setSound(soundUri)
            .setContentIntent(pendingIntent)
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.notify(count, notificationBuilder.build())
        count += 1
    }
}

如何修改我的应用程序在后台工作?

始终在通知API调用中发送数据对象这样:

{  
    "data" : {
      "body" : "Watch Live Footlball Streaming online & stay updated with fastest live match scores on Hotstar",
            "content_available" : true,
            "title" : "Brazil vs. Denmark",
            "isScheduled":false,
            "scheduledTime":"2020-03-03 12:20:00",
            "stationCode":"PNB",
            "groupId":2
    },
  "to" : "epGANq1gDpE:APA91bHEuHS_QRILwI9GJ-x8Sz4DKjHRN0KX23R5TGx9HyRAN8caghmakMSnRavOf4Qwnmni3ByVRndvMjmMvfQnVt-2g6AgI5OB1-0vK3HQWDdhAjb155KWnYLXgEn763hSSbPX7tFm",
    "priority" : "high"
}

另外,检查此样本以在后台运行。https://github.com/googlecodelabs/android-kotlin-notifications-fcm

最后,我找到了下面的解决方案:

class MyFirebaseMessagingService : FirebaseMessagingService() {
    val TAG = "FirebaseMessagingService"
    var count = 0
    @SuppressLint("LongLogTag")
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        if (remoteMessage.data != null && remoteMessage.data.isNotEmpty()) {
            showNotification(remoteMessage.data)
        }
    }
    private fun showNotification(data :Map<String,String>) {
        var intent = Intent()
        var title =data["title"]
        var body =data["body"]
        var activityName = data["view"]
        if (activityName == "news")
        {
            intent = Intent(this, NewsDetailesActivity::class.java)
            var id = data["id"]
            NewsDetailesActivity.currentNewId = id!!
        }
      
        else if(activityName == "url") {
            val url  = data["url"]
            val b = Bundle()
            val uris = Uri.parse(url)
            intent = Intent(Intent.ACTION_VIEW, uris)
            b.putBoolean("new_window", true)
            intent.putExtras(b)
        }

        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        val pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)

        val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val notificationBuilder = NotificationCompat.Builder(this, "channelId")
            .setSmallIcon(R.drawable.logo_orang)
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true)
            .setSound(soundUri)
            .setContentIntent(pendingIntent)
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.notify(count, notificationBuilder.build())
        count += 1
    }
}

在我的API上,我删除了通知密钥,只发送数据有效载荷。

相关内容

  • 没有找到相关文章

最新更新