Ionic / Capacitor - PushNotiifcations - Android - “notification” + “data” message

2.1k Views Asked by At

I am using PushNotifications Capacitor plugin and FCM to send the notifications, everything works fine until I want to send a Push Notification with both notification and data fields on Android.

What I want to do is to send a custom data with the notification and consume the notification on app resume. I am using PushNotifications.getDeliveredNotifications() method to get the notifications on resume, on iOS I get all the notifications with proper data, but on Android data property is replaced with the object below:

{
  body: "My Body",
  data: {
    android.appInfo: "ApplicationInfo{809371c app}",
    android.bigText: "My Body",
    android.progress: 0,
    android.progressIndeterminate: false,
    android.progressMax: 0,
    android.reduced.images: true,
    android.showChronometer: false,
    android.showWhen: true,
    android.template: "android.app.Notification$BigTextStyle",
    android.text: "My Body",
    android.title: "My Title",
    gameDndOn: false,
    specialType: "",
    topFullscreen: false,
    groupSummary: false,
    id: 0,
    title: "My Title"
  }
}

I see in the FCM docs for Android apps that if both notification and data fields are present and the app is in the background the notification lands in the system tray and data in extras of the intent.

I am getting proper data when I tap on the notification, but in my case I want to get that on resume, without tapping on the notification.

Is there any way to get to that data in that case?

1

There are 1 best solutions below

2
On

You have to create the message in this format

{
"message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    },
    "data" : {
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  }
}

Note : notification structure is critical for receiving a notification. You need to have title and body. This gets displayed in the notification drawer.

data can be customized as per your requirement. This will have the data that you need inside your app.

Suggestion, keep your payload small. Send data which is absolutely necessary and also safe to send over FCM.

Look at this part of the official documentation

For handling push notifications in capacitor, you should use the listeners. Here is the official docs way of doing it.