React-native-push-notification: onNotification method

Created on 2 Jun 2017  路  9Comments  路  Source: zo0r/react-native-push-notification

PushNotification.configure({

  onNotification: function (notification) {
    console.log('NOTIFICATION:', notification)
     PushNotification.localNotification({
      largeIcon: "ic_launcher",
      title: notification.title, 
      message: notification.message, 
    });

  },
  senderID: "my sender ID",
  popInitialNotification: true,
  requestPermissions: true,
});

I am Getting notification from server. After receiving the notification, When I click on it, then again the same notification is coming. More I click on it more the notifications are coming.

My question is whether I am following the right approach in the above code? If not then please suggest me the correct one. How to handle the click of notification? So, that i can show the particular view on its click.

Thanks in advance :)

Stale

Most helpful comment

it looks like you've got an infinite loop of sorts! there's no need to a local notification inside your onNotification method.

All 9 comments

@npomfret Hello can you help ? :)

it looks like you've got an infinite loop of sorts! there's no need to a local notification inside your onNotification method.

Thanks a lot ! @npomfret
So, where to put this method ?

@npomfret I've been trying to make sense of this too. The below configuration works as expected for both ios and android.

PushNotification.configure({
  onRegister(token) {
    registerDevice(token);
  },
  onNotification(notification) {
    if (Platform.OS === 'android') {
      PushNotification.localNotification({
        message: notification.msg,
        largeIcon: 'ic_launcher',
        smallIcon: 'ic_notification',
      });
    }
  },
  senderID: 'GCM ID'
});

If I remove PushNotification.localNotification({}) from onNotification. Android devices don't display a notification when the app is in the Background or Killed. However, if I add a console.log() the message is logged.

Also, calling PushNotification.localNotification({}) from 'onNotification' prevents ios notifications

Thanks in advance!

Hello @aaronallan ,

I have tried the following approach to solve this problem

PushNotification.configure({

      onNotification: function (notification) {
        console.log('NOTIFICATION:', notification)
        const clicked = notification.userInteraction;
        if (clicked) {
         ToastAndroid.show(notification.message,ToastAndroid.CENTER);
        } else {
          PushNotification.localNotification({
            largeIcon: "ic_launcher",
            title: "Test",
            //message: JSON.stringify(xyz.notificationResponse.bookingId), 
          });
        }
        ToastAndroid.show(notification.message,ToastAndroid.CENTER);
      },


      senderID: "your sender id",
      popInitialNotification: true,
      requestPermissions: true,
    });

Hello @npomfret ,

I have used the solution which you were trying to explain and its working fine at my end. My next case is , if the application is opened and notification comes from server then i want to show a particular view in my home. screen. I have checked data in the notification is coming from server.

````
PushNotification.configure({

  onNotification: function (notification) {
    console.log('NOTIFICATION:', notification)
    const clicked = notification.userInteraction;
    if (clicked) {

//Open the app when I click on the notification
ToastAndroid.show(notification.message,ToastAndroid.CENTER);
} else {
PushNotification.localNotification({
largeIcon: "ic_launcher",
title: "Test",
//message: JSON.stringify(xyz.notificationResponse.bookingId),
});
}
// This case works when I am already in the application and I want to change the view in this case
ToastAndroid.show(notification.message,ToastAndroid.CENTER);
},

  senderID: "your sender id",
  popInitialNotification: true,
  requestPermissions: true,
});`

```

Please Help !

Thanks in advance.. :)

if (!notification.localNotification) {
    PushNotification.localNotification({
      title: 'Title',
      localNotification: true,
      message: i18n.t(notification.message),
      postId: notification.postId,
    });

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions.

if (!notification.localNotification) {
  PushNotification.localNotification({
    title: 'Title',
    localNotification: true,
    message: i18n.t(notification.message),
    postId: notification.postId,
  });

@jurajkrivda How are you?
I have seen that you use i18n inside react-native to translate the push, did you try using strings.xml in android studio with "title_loc_key" and "body_loc_key"? Because it is not working to me and it works great the same approach on iOS. (I tried i18n from react and it works great but we have to code that and parse the parameteres and we prefer to avoid that). Thanks!

Was this page helpful?
0 / 5 - 0 ratings