react-native-push-notification icon indicating copy to clipboard operation
react-native-push-notification copied to clipboard

Which method is used when we press a only local notification comes when app is in foreground?

Open Haseeba393 opened this issue 3 years ago • 10 comments

Question

I'm using Firebase for push notifications in React Native. As you know that when the application is in the foreground then notification does not show in the notification tray but we can receive in onMessage() Handler provided by firebase. So to show notification in notification tray, I'm using this library to manually produce local notification. So what I need right now when the user is using the application and the application is in the foreground, clicking on that notification will redirect the user to a specific screen in the application. Can anybody please help me out?

Haseeba393 avatar Mar 18 '21 05:03 Haseeba393

Maybe you need to use React Navigation and the method onAction or onNotification of React Native Push Notification.

Phosphorus-M avatar Mar 20 '21 18:03 Phosphorus-M

Both of these methods work for remote notifications too which I do not want to do. Through this, I'm integrating local push notifications but the methods you are saying if I will implement those then they will trigger as well on remote notifications which I do not want to do.

Haseeba393 avatar Apr 01 '21 07:04 Haseeba393

In my case, the app start popping local push notification after I send a remote push notification in background state. I don't know why but after this the local push notification started works.

Hermanyo avatar Apr 05 '21 15:04 Hermanyo

I solve it: android/app/src/main/AndroidManifest.xml

<meta-data
      android:name="com.dieam.reactnativepushnotification.default_notification_channel_id"
      android:value="default_channel_name" />

and...

useEffect(()=>
 PushNotification.createChannel(
      {
        channelId: 'default_channel_name', // (required)
        channelName: 'default_channel_name', // (required)
      },
      created => console.debug(`createChannel returned '${created}'`),
    );

 const unsubsceribeRemoteMessaging = firebase
      .messaging()
      .onMessage(message => {
        PushNotification.localNotification({
          ...message,
          ...message.data,
          ...message.notification,
          channelId: 'default_channel_name'',
          message: message.notification.body,
          ignoreInForeground: false, 
          smallIcon: 'ic_launcher',
          // your optionals props... 
        });
      });

     return () => {
      unsubsceribeRemoteMessaging();
    };
},[])

Hermanyo avatar Apr 05 '21 17:04 Hermanyo

Hermanyo in this case, we can generate the local notification that is fine. What I want clicking on local push notification will go to another screen and clicking remote notification will take it to another screen. I want click push notification handler will only work for local push notification. Right now, it's working for both local and push notification.

On Mon, Apr 5, 2021, 10:32 PM Hermanyo H. @.***> wrote:

I solve it: android/app/src/main/AndroidManifest.xml

and...

useEffect(()=> PushNotification.createChannel( { channelId: 'default_channel_name', // (required) channelName: 'default_channel_name', // (required) }, created => console.debug(createChannel returned '${created}'), );

const unsubsceribeRemoteMessaging = firebase .messaging() .onMessage(message => { PushNotification.localNotification({ ...message, ...message.data, ...message.notification, channelId: 'default_channel_name'', message: message.notification.body, ignoreInForeground: false, smallIcon: 'ic_launcher', // your optionals props... }); });

 return () => {
  unsubsceribeRemoteMessaging();
};

},[])

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/zo0r/react-native-push-notification/issues/1913#issuecomment-813522502, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJ3R4UBZNPZK55YCWO7JPA3THHX3FANCNFSM4ZL6QI3Q .

Haseeba393 avatar Apr 06 '21 02:04 Haseeba393

Hi @Haseeba393 , I have implemented this library for local notification and it doesn't have separate methods for Remote or local notifications, you can only catch both in one method i.e

// (required) Called when a remote or local notification is opened or received
  onNotification: handler.onNotification.bind(handler),

I'm struggling with redirecting the user to a specific screen, when i receive a notification and on click of that , i want user to redirect to particular screen. I have followed this example only when you schedule in particular class, the callback methods are executing. I have login screen in middle, how do i take user to particular screen where notification schedule happened at some other class. Any idea, please help, Thanks.

Madhu02 avatar Apr 06 '21 11:04 Madhu02

Actually brother I'm implementing Push notifications using Firebase and Firebase onMessage() handler is only catching the notification but not showing any notification in the notification tray. The only thing I can do is to implement Local Notification But implementing Local Notification means cathing both local and remote notification which I don't want. Regarding your question, how to redirect to a specific screen, then the best way is to put handlers to the very start of the application. Then get the notification on which you are clicking in the respective handler and then using navigation.navigate() or using deep linking you can navigate to specific screen.

On Tue, 6 Apr 2021 at 16:59, Madhu Kunal @.***> wrote:

Hi @Haseeba393 https://github.com/Haseeba393 , I have implemented this library for local notification and it doesn't have separate methods for Remote or local notifications, you can only catch both in one method i.e

// (required) Called when a remote or local notification is opened or received onNotification: handler.onNotification.bind(handler),

I'm struggling with redirecting the user to a specific screen, when i receive a notification and on click of that , i want user to redirect to particular screen. I have followed this example https://github.com/zo0r/react-native-push-notification/tree/master/example only when you schedule in particular class, the callback methods are executing. I have login screen in middle, how do i take user to particular screen where notification schedule happened at some other class. Any idea, please help, Thanks.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/zo0r/react-native-push-notification/issues/1913#issuecomment-814062799, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJ3R4UA6HOECKK2BWIIADFDTHLZS3ANCNFSM4ZL6QI3Q .

Haseeba393 avatar Apr 06 '21 13:04 Haseeba393

You have probably solved it, but i'll leave a solution for someone who might need it. You can use "foreground" property of notification object passed to onNotification and only apply your logic if foreground: true.

waryiar avatar Aug 13 '21 10:08 waryiar

@warryr No, I was not able to solve this problem. What I have done that on receiving notification, I was updated application UI according to it rather than displaying notification in notification tray

Haseeba393 avatar Aug 13 '21 13:08 Haseeba393

Any solution for this issue?

rutvikatharva avatar Sep 01 '23 12:09 rutvikatharva