jordan smith
jordan smith

Reputation: 15

Firebase cloud messaging in android application only working for notifications and not data messages

Using firebase to send push notification to android device.

<service
     android:name=".util.MyFirebaseMessagingService"
     android:exported="false">
     <intent-filter>
           <action android:name="com.google.firebase.MESSAGING_EVENT" />
     </intent-filter>
</service>


class MyFirebaseMessagingService: FirebaseMessagingService()  {

    private val TAG = "FirebaseMessaging"

    override fun onNewToken(token: String) {
        super.onNewToken(token)
        Log.d(TAG, token)
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)

        if (remoteMessage.notification != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.notification!!.body)
        }
    }
}

I can receive notifications of a message by using postman.

{
    "registration_ids": ["KEY_HERE"],
    "notification": {
        "title": "HIMAN",
        "body": "werwkejh"
    }
}

This works perfectly, I can add a break-point and the notification comes through. However, I need this to be a data type push notification because it doesn't trigger the onMessageReceived callback when the app is in the background - only foreground. It says it's successful too in postman.

{
    "multicast_id": 863107467701827657,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [
        {
            "message_id": "MESSAGE_ID"
        }
    ]
}

The request body for the data push notification is as follows:

{
    "registration_ids": ["KEY_HERE"],
    "data":{
        "message": "hi!"
    }
}

It's just not triggering onMessageReceived which I need. Anybody can help?

Upvotes: 1

Views: 322

Answers (2)

Jaydeep Khambhayta
Jaydeep Khambhayta

Reputation: 5279

when application close system will handle notification if notification payload have notification key that will auto display notification based on that. below payload display notification title and body when app close and if you handle data json it will not show until app started.

{
    "to":"some_device_token"
    "notification": {
        "title": "HIMAN",
        "body": "werwkejh"
    },"data": {
           "extra":"juice"
}
}

Solution for this problem is to remove notification from payload that will allow application to handle the json.

{
"to":"some_device_token"
"data": {
"extra":"juice"
}
}

**

if payload contain notification key system will handle when app close, if payload not contain notification key application will handle it.

**

Upvotes: 1

Rahul sharma
Rahul sharma

Reputation: 1574

If your notification contain "notification": it will show notification but when app is not running then onMessageReceived will not call. for that case you have pass data in "data": field and remove notification": from you response.

Your response must be look like this

{
"to": "your device token",
 "data":{
   "name": "John",
    "message": "Hii! "
    }
 }

I had same issue this worked for me.

Upvotes: 1

Related Questions