wix / react-native-notifications

React Native Notifications
MIT License
3.25k stars 764 forks source link

notification title & body = undefined #858

Closed codeheart09 closed 2 years ago

codeheart09 commented 2 years ago

I have version 4.2.4 installed in Android 11.

When I receive a notification in the foreground, the Notification object does not have a title or body. I'm using FCM's test message.

Notifications.events().registerNotificationReceivedForeground(
  (notification, completion) => {
    console.log(`Notification received in foreground: ${notification.title} : ${notification.body}`);
    console.log(notification);
    completion({ alert: true, sound: true, badge: true});
  },
);

This outputs:

Notification received in foreground: undefined : undefined
{"identifier": "0:1650400098363430%d3631f10d3612f11", "payload": {"collapse_key": "com.app.myapp", "from": "9999999999", "gcm.n.dnp": "1", "gcm.n.e": "1", "gcm.notification.body": "Please git commit, push and run!", "gcm.notification.e": "1", "gcm.notification.tag": "campaign_collapse_key_99999999999999", "gcm.notification.title": "Fire on the building!", "google.c.a.c_id": "9999999999999", "google.c.a.c_l": "DevTest", "google.c.a.e": "1", "google.c.a.ts": "1650400078", "google.c.a.udt": "0", "google.c.sender.id": "9999999999999", "google.delivered_priority": "high", "google.message_id": "0:1650400098363430%d3631f10d3612f11", "google.original_priority": "high", "google.sent_time": 1650400078351, "google.ttl": 2419200}}

As you can see, the message is present in the payload, but not in the object.

Because of this, the notification does not show up. Only workaround is to get the data from the payload and trigger a local notification.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

stale[bot] commented 2 years ago

The issue has been closed for inactivity.

ajaybushel commented 2 years ago

Any updates on this issue? I am facing the same problem. The output varies depending on the type of event.

ashokkumar88 commented 1 year ago

@ajaybushel Did you find any workaround? I am facing same issue.

ajaybushel commented 1 year ago

@ashokkumar88 This is what I ended up doing:

if (notification) {
  const {
    payload: { action, title, body },
  } = notification;

  const action =
    notification.payload["gcm.notification.action"] ||
    notification.payload.action;
  const title =
    notification.payload["gcm.notification.title"] ||
    notification.payload.title;
  const body =
    notification.payload["gcm.notification.body"] ||
    notification.payload.body;
}
chenweigh commented 1 year ago

same issue +1 and When I receive a notification in the background, then open it to launch app, the Notification object does not have a title or body.

{ 
collapse_key: 'com.libeccio.xterio',
'gcm.n.analytics_data': 
      { 'google.c.a.e': '1',
          'google.c.a.c_l': 'test',
        'google.c.a.udt': '0',
        'google.c.a.ts': '1695199063',
        from: '1022924363809',
         'google.c.a.c_id': '6809650627476227774'
 },
'google.message_id': '0:1695199063520892%c4b7d40ac4b7d40a',
'google.original_priority': 'high',
'google.ttl': 2419200,
 from: '1022924363809',
 'google.sent_time': 1695199063512,
'google.delivered_priority': 'high' }

there are no 'gcm.notification.title' key

@ajaybushel

ajaybushel commented 1 year ago

@chenweigh As far as I remember, I had to pass the notification data in the "extraData" field or something from the notification hub itself. On iOS, the library properly picks up the title and body from notification.data object but on Android, for some reason, it doesn't.

Because of this issue, we ended up sending notifications (using Azure Notification Hub) like:

{
  "data": {
    "title": "...",
    "body": "..."
  },
  "payload": {
    "title": "...",
    "body": "..."
  }
}

Once done, I was able to properly access the properties on both iOS and Android. Also, I wouldn't take this concretely as it has been sometime since I worked on this.