FCM shows default message than custom message when app is not running

前端 未结 2 711
时光说笑
时光说笑 2020-12-12 06:01

Problem: My FCM code shows different messages to different cases:

Case 1: When app is running it shows the custom notification body

Case 2:

相关标签:
2条回答
  • 2020-12-12 06:23

    From the document

    When app is in foreground the onMessageReceived always calls.

    When app is in background the onMessageReceived will be called when the payload only has the data property (it doesn't exist the notification property).

    Hope it helps!

    0 讨论(0)
  • 2020-12-12 06:42

    This is expected. FCM has different behaviours for app status (foreground and background / killed). You should handle this by the payload you sent from server, according to your use case.

    The msg sent from server has to be sent in either "notification" or "data" format, from dashboard or server side api. Note: From firebase dashobard you can only send "notification" body and not data. In such cases, FCM will directly display the notif without giving a callback to your app.

    Server side Below are sample formats :

    Notification Type Format Note : Android System will by default display the notification in the notification tray and you don't need to display it.

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

    Data Format (For receiving callback in app, in foreground and background) Note : You have to handle callback and display notif on your own.

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

    Android Client To handle the payload received in your Android receiver, checl the official guide 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.
    }
    

    Check the documentation here

    0 讨论(0)
提交回复
热议问题