react-native-notifications icon indicating copy to clipboard operation
react-native-notifications copied to clipboard

Handle callback when notification in background / terminate app

Open muslimmuda15 opened this issue 1 year ago • 4 comments

How to handle callback when Notificationin background?

I have put the code into index.js

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

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

The Notifications.events().registerNotificationReceivedForeground cllback in working well. when I using Notifications.events().registerNotificationReceivedBackground the notif is successful to show in background / terminate, but I cannot getting any callback in my log. Please help

muslimmuda15 avatar May 21 '24 10:05 muslimmuda15

Same here , Please provide the fix

SwapnilRathore avatar Jun 14 '24 07:06 SwapnilRathore

I am observing the following:

  • On emulator, the notification is shown on foreground and background. HOWEVER, only registerNotificationReceivedForeground logs. registerNotificationReceivedBackground does not log.
  • A colleague testing on hardware sees notifications on foreground, not on background.

This is on v5.1.0. Really puzzling.

I have set <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> in all AndroidManifest, but this was already present in debug build and did not change anything.

I also ensured that playImplementation "com.google.firebase:firebase-messaging:23.3.1", not core, was present.

coyotespike avatar Jul 18 '24 23:07 coyotespike

Just to bump this: this bug appears to be related to a lack of push notifications on Android API 33 and above, when the app is in the background. If anyone has any ideas I am happy to debug further!

coyotespike avatar Jul 23 '24 02:07 coyotespike

Same problem here in iOS. registerNotificationReceivedBackground does not log.

khalilpan avatar Sep 04 '24 12:09 khalilpan

Any update on this ?

nlabofa avatar Oct 19 '24 12:10 nlabofa

Hi! I have the same problem on android, but I found that if send message with only field "data"(without "notification" field) I can handle callback when app in background, but if app terminate it not worked! If exist "notofication" field, I can't handle in background and terminate mode. for ex send throught postman+firebase:

{
    "message":{
        "token":"xxx",
        "data": {
            "text": "Hello!"
        }
    }
}

Any solutions?

PS If I override FcmService on native, I can handle all message on all modes in onMessage method but then Notifications.events().. can't work.

PS2 I understood the problem. It's can't work when app terminated because rn-notification need React Context but if app terminated ReactContext is null and then method on JS can't fire from native code.

van8tka avatar Nov 22 '24 11:11 van8tka

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] avatar Jan 31 '25 22:01 stale[bot]

The issue has been closed for inactivity.

stale[bot] avatar Feb 25 '25 00:02 stale[bot]