android:根据FCM通知的数据执行操作

时间:2019-08-04 13:38:34

标签: java android kotlin notifications firebase-cloud-messaging

我正在开发一个使用Firebase云消息传递来接收通知的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

    }
}

如何修改我的应用程序使其在后台运行?

2 个答案:

答案 0 :(得分:0)

最后我找到了如下解决方案:

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 == "niaba")
        {
            intent = Intent(this, NiabaaActivity::class.java)
        }
        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上,我删除了通知密钥,只发送数据有效载荷。

答案 1 :(得分:0)

始终在通知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