【问题标题】:FCM shows default message than custom message when app is not running当应用程序未运行时,FCM 显示默认消息而不是自定义消息
【发布时间】:2019-10-28 07:09:25
【问题描述】:

问题:我的 FCM 代码针对不同的情况显示不同的消息:

案例 1:应用程序运行时显示自定义通知正文

案例 2:当应用在后台运行时,它会显示自定义通知正文

案例 3:当应用程序未运行时,它会显示从 FCM 收到的默认消息,而不是自定义消息

代码:

/* The class extends FirebaseMessagingService() */

override fun onMessageReceived(remoteMessage: RemoteMessage) {
        try {
/* Some other codes */            

            val pendingIntent = PendingIntent.getActivity(this, 0, Intent(), PendingIntent.FLAG_ONE_SHOT)
            val builder = NotificationCompat.Builder(this, "" + R.string.notification_channel_id)
                    .setSmallIcon(R.mipmap.ic_launcher_round)
                    .setContentTitle(remoteMessage.notification!!.title)
                    .setContentText(getString(R.string.custom_message_body))
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
            val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val channel = NotificationChannel("" + R.string.notification_channel_id, "Alert channel", NotificationManager.IMPORTANCE_DEFAULT)
                manager.createNotificationChannel(channel)
            }
            manager.notify(0, builder.build())

/* super.onMessageReceived(remoteMessage) was REMOVED to prevent default functions */
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

【问题讨论】:

    标签: android kotlin firebase-cloud-messaging


    【解决方案1】:

    这是意料之中的。 FCM 对应用程序状态(前台和后台/已终止)有不同的行为。 根据您的用例,您应该通过从服务器发送的有效负载来处理此问题。

    从服务器发送的消息必须以“通知”或“数据”格式从仪表板或服务器端 API 发送。 注意:从 firebase dashobard 您只能发送“通知”正文而不是数据。在这种情况下,FCM 会直接显示通知而不给您的应用回调。

    服务器端 以下是示例格式:

    通知类型格式 注意:Android系统默认会在通知托盘中显示通知,您不需要显示。

     { 
        "to": "your_token_id",
         "notification" : {
                 "title" : "FCM Notification title!",
                 "body" : "FCM Notification subtext!",
                 "content_available" : true,
                 "priority" : "high"
         }
    }
    

    数据格式(用于在应用中接收回调,在前台和后台) 注意:你必须自己处理回调和显示通知。

    { 
        "to": "your_token_id",
    
        "data" : {
             "title" : "FCM Notification Title ",
             "subtext" : "FCM Notification Sub Title",
             "type" : "999",
             "priority" : "high"
        }
    }
    

    Android 客户端 要处理在您的 Android 接收器中收到的有效负载,请查看官方指南 here

    /* The class extends FirebaseMessagingService() */   
     override fun onMessageReceived(remoteMessage: RemoteMessage) {
    
            Log.d(TAG, "From: ${remoteMessage.from}")
    
            // Check if message contains a data payload.
            remoteMessage.data.isNotEmpty().let {
                Log.d(TAG, "Message data payload: " + remoteMessage.data)
    
            if (/* Check if data needs to be processed by long running job */ true) {
                // For long-running tasks (10 seconds or more) use WorkManager.
                scheduleJob()
            } else {
                // Handle message within 10 seconds
                handleNow()
            }
        }
    
        // Check if message contains a notification payload.
        remoteMessage.notification?.let {
            Log.d(TAG, "Message Notification Body: ${it.body}")
        }
    
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
    }
    

    查看文档here

    【讨论】:

    • 奇怪的是 onMessageReceived 被调用但系统忽略了我的自定义通知..感谢有用的信息
    【解决方案2】:

    来自the document

    当应用程序在前台时,onMessageReceived 总是调用。

    当应用程序在后台时,当负载只有data 属性(不存在notification 属性)时,onMessageReceived 将被调用。

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多