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?
data
it should work regardless if background or foreground, add a log inside onmessagerecieved to check – Peter Haddad