React-native-push-notification: Notification not cleared and got error while clicking FCM or Remote message

Created on 19 Jul 2020  路  5Comments  路  Source: zo0r/react-native-push-notification

react-native and library version :

 "react": "16.13.1",
  "react-native": "0.63.1",
  "react-native-push-notification": "^4.0.0",

1. I am having a problem of clearing notification when user click on the notification which was sent from FCM console or Remote FCM message. I tried this

PushNotification.cancelLocalNotifications({ id: this.lastId});
PushNotification.cancelLocalNotifications({ id: '' + this.lastId });

But none of these worked.

2. When user taps the notification to open application, i am getting error undefined is not an object.
Below are the errors

Screenshot_1595137819
Screenshot_1595138081

image

What are the proper way to handle notification, FCM and FCM remote message ?

Most helpful comment

@vishwa1937 as i explained in the other topic, when the app is open, you get the notification and the data is received through the data{} object, but if the app is closed or minimized! and you click on the notification to open the app but the data doesn't come from the data{} object, you have to access it directly

onNotification(notification) {
        if (notification.data) { //// **APP IS OPEN**
            notification.data.title
            notification.data.message
        } else { ///////**APP IS CLOSED!!!**
            notification.title
            notification.message
        }
 },

All 5 comments

Hi,

  1. Why are you using this.lastId for a remote notification? The notification id is in the object notification.id. And the notification should also disappear after the click.
  2. You are sending a notification without data and you are trying to access the data object. Print the content of the object notif and find what you want. It's probably notif.title.

Regards,

Hello, @Dallas62 Thank you for you reply. I tried as you said above, and it is working now. Thank you so much.

  1. I wondered i did not found about notification.id in documentation.
  2. It is also working now, i used for notf.data.title for remote message using https://fcm.googleapis.com/fcm/send and notif.title for FCM console message. I flipped the condition and its working now.

Now, I am facing another problem, I am not getting notification when app is closed i.e in background from FCM remote message https://fcm.googleapis.com/fcm/send but getting message sent from FCM console.

Thank you

For the problem in background, it has been documented in many issues, even recent one

@vishwa1937 as i explained in the other topic, when the app is open, you get the notification and the data is received through the data{} object, but if the app is closed or minimized! and you click on the notification to open the app but the data doesn't come from the data{} object, you have to access it directly

onNotification(notification) {
        if (notification.data) { //// **APP IS OPEN**
            notification.data.title
            notification.data.message
        } else { ///////**APP IS CLOSED!!!**
            notification.title
            notification.message
        }
 },

@luigbren , @Dallas62 Thank you so much for your reply. Now, I am able to get notification in foreground, background, and app closed state.

Previously, i used different JSON format of remote message i.e
{ "data": { "title": "Hello John Doe", "message": "How are you? How you doing?" }, "to": "/topics/fcmDemo" }

I changed this format to :
{ "notification": { "title": "Hello John Doe", "body": "How are you? How you doing?" }, "data": { "badge": 35, "payload": { "message": "This is payload message", "url": "This is payload url" } }, "to": "/topics/fcmDemo" }

I am able to get FCM notification form FCM console and remote message using https://fcm.googleapis.com/fcm/send.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

selimonline picture selimonline  路  3Comments

NiuQiaoling picture NiuQiaoling  路  3Comments

ssolida picture ssolida  路  3Comments

anativ picture anativ  路  3Comments

Kiran0791 picture Kiran0791  路  3Comments