zetavg / react-native-system-notification

Android system notifications for React Native. Supports push notifications with GCM integrated.
https://www.npmjs.com/package/react-native-system-notification
244 stars 102 forks source link

BACKGROUND ISSUE: launching app, that is running in background didnt trigger press event listener #71

Open rimzici opened 7 years ago

rimzici commented 7 years ago

node modules versions: "react": "^15.2.1", "react-native": "^0.31.0", "react-native-system-notification": "^0.2.0", "react-native-fcm": "^1.0.16",

tried with "react-native": "^0.29.0", also. have made all config changes told in react-native-system-notification docs.added android:launchMode="singleTask" in manifest file. and issue of app loading every time , even when launching from background got solved, now it resumes. So after that the event listener is not working. Notification.addListener('press', function(e) { console.log(e); }); the listener works when app is running in the foreground. But in the background app launches on clicking notification but event listener not triggered. Any Suggestions ? Thanks in advance.

rimzici commented 7 years ago

pasting the debug results here: would appreciate if the team can suggest what Iam doing wrong. ON PRESSING CUSTOM NOTIFICATION:

  1. App Running In Foreground : LISTENER TRIGGERED 08-23 22:49:00.602 20000-20000/com.testsample D/ActivityThread: BDC-Calling onReceive: intent=Intent { flg=0x10 cmp=com.testsample/io.neson.react.notification.NotificationEventReceiver (has extras) }, receiver=io.neson.react.notification.NotificationEventReceiver@3071bc5c 08-23 22:49:00.602 20000-20000/com.testsample I/ReactSystemNotification: NotificationEventReceiver: Recived: DEFAULT, Notification ID: 1337, payload: {"number":1,"what":true,"someAnswer":"42"} 08-23 22:49:00.603 20000-20000/com.testsample V/ReactSystemNotification: NotificationEventReceiver: ok: com.testsample 08-23 22:49:00.604 20000-20000/com.testsample V/ReactSystemNotification: NotificationEventReceiver: ok: com.testsample 08-23 22:49:00.604 20000-20000/com.testsample I/ReactSystemNotification: NotificationEventReceiver: Launching: TEST ELSE IF 08-23 22:49:00.608 20000-20000/com.testsample V/ReactSystemNotification: NotificationEventReceiver: Broadcast Sent: NotificationEvent: DEFAULT, Notification ID: 1337, payload: {"number":1,"what":true,"someAnswer":"42"} 08-23 22:49:00.608 20000-20000/com.testsample D/ActivityThread: BDC-RECEIVER handled : 0 / ReceiverData{intent=Intent { flg=0x10 cmp=com.testsample/io.neson.react.notification.NotificationEventReceiver (has extras) } packageName=com.testsample resultCode=0 resultData=null resultExtras=null} 08-23 22:49:00.609 20000-20000/com.testsample W/unknown:React: Calling JS function after bridge has been destroyed. 08-23 22:49:00.609 20000-20000/com.testsample I/ReactSystemNotification: NotificationModule: sendEvent (to JS): sysModuleNotificationClick 08-23 22:49:00.612 20000-20000/com.testsample I/ReactSystemNotification: NotificationModule: sendEvent (to JS): sysModuleNotificationClick 08-23 22:49:00.613 20000-23519/com.testsample I/ReactNativeJS: TEST : 08-23 22:49:00.627 20000-20000/com.testsample D/wangcy9: setStatusIcon occur wrong theme! 08-23 22:49:00.653 20000-20000/com.testsample D/ViewRootImpl: hardware acceleration is enabled, this = ViewRoot{a834354 com.testsample/com.testsample.MainActivity,ident = 3} 08-23 22:49:00.659 20000-20000/com.testsample D/Dialog: show 08-23 22:49:00.686 20000-20025/com.testsample D/OpenGLRenderer: CanvasContext() 0xab075880 initialize 0xaae3a4f8 08-23 22:49:00.686 20000-20025/com.testsample D/Surface: Surface::connect(this=0xaae3a4f0,api=1) 08-23 22:49:00.687 20000-20025/com.testsample D/mali_winsys: new_window_surface returns 0x3000 08-23 22:49:00.688 20000-20025/com.testsample D/Surface: Surface::allocateBuffers(this=0xaae3a4f0)
    1. App not running/killed: LISTENER TRIGGERED 08-23 22:55:07.626 25764-25809/com.testsample I/ReactNativeJS: Running application "testsample" with appParams: {"initialProps":{},"rootTag":1}. DEV === true, development-level warning are ON, performance optimizations are OFF 08-23 22:55:07.627 25764-25790/com.testsample D/Surface: Surface::setBuffersDimensions(this=0xaaf52e60,w=1080,h=1920) 08-23 22:55:07.628 25764-25790/com.testsample D/GraphicBuffer: register, handle(0xaafa7ab0) (w:1080 h:1920 s:1088 f:0x1 u:0x000b00) 08-23 22:55:07.736 25764-25809/com.testsample I/ReactNativeJS: TEST : 08-23 22:55:07.747 25764-25764/com.testsample D/wangcy9: setStatusIcon occur wrong theme! 08-23 22:55:07.754 25764-25810/com.testsample V/NotificationManager: com.testsample: notify(1337, Notification(pri=1 contentView=com.testsample/0x1090079 vibrate=default sound=default defaults=0x7 flags=0x11 color=0x00000000 vis=PRIVATE)) 08-23 22:55:07.757 25764-25810/com.testsample I/ReactSystemNotification: Notification Show: 1337 08-23 22:55:07.757 25764-25810/com.testsample I/ReactSystemNotification: Notification Created: 1337 08-23 22:55:07.770 25764-25764/com.testsample D/ViewRootImpl: hardware acceleration is enabled, this = ViewRoot{3a0b62c3 com.testsample/com.testsample.MainActivity,ident = 1} 08-23 22:55:07.775 25764-25764/com.testsample D/Dialog: show 08-23 22:55:07.810 25764-25790/com.testsample D/OpenGLRenderer: CanvasContext() 0xab0a6ba0 initialize 0xaae37a58 08-23 22:55:07.810 25764-25790/com.testsample D/Surface: Surface::connect(this=0xaae37a50,api=1) 08-23 22:55:07.812 25764-25790/com.testsample D/mali_winsys: new_window_surface returns 0x3000 08-23 22:55:07.813 25764-25790/com.testsample D/Surface: Surface::allocateBuffers(this=0xaae37a50)
    2. App is Running In the Background: LISTENER NOT-TRIGGERED

08-23 22:56:14.547 25764-25764/com.testsample D/ActivityThread: BDC-Calling onReceive: intent=Intent { flg=0x10 cmp=com.testsample/io.neson.react.notification.NotificationEventReceiver (has extras) }, receiver=io.neson.react.notification.NotificationEventReceiver@77a82b3 08-23 22:56:14.548 25764-25764/com.testsample I/ReactSystemNotification: NotificationEventReceiver: Recived: DEFAULT, Notification ID: 1337, payload: {"number":1,"what":true,"someAnswer":"42"} 08-23 22:56:14.553 25764-25764/com.testsample I/ReactSystemNotification: NotificationEventReceiver: launchIntent: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x14000000 pkg=com.testsample cmp=com.testsample/.MainActivity (has extras) } 08-23 22:56:14.554 25764-25764/com.testsample I/ReactSystemNotification: NotificationEventReceiver: context: android.app.ReceiverRestrictedContext@1c640a30 08-23 22:56:14.578 25764-25764/com.testsample I/ReactSystemNotification: NotificationEventReceiver: Launching: com.testsample 08-23 22:56:14.579 25764-25764/com.testsample D/ActivityThread: BDC-RECEIVER handled : 0 / ReceiverData{intent=Intent { flg=0x10 cmp=com.testsample/io.neson.react.notification.NotificationEventReceiver (has extras) } packageName=com.testsample resultCode=0 resultData=null resultExtras=null} 08-23 22:56:14.581 25764-25790/com.testsample D/OpenGLRenderer: Flushing caches (mode 0) 08-23 22:56:14.581 25764-25790/com.testsample D/OpenGLRenderer: Flushing caches (mode 0) 08-23 22:56:14.587 25764-25764/com.testsample D/ActivityThread: ACT-NEW_INTENT handled : 0 / NewIntentData{intents=[Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x14400000 pkg=com.testsample cmp=com.testsample/.MainActivity (has extras) }] token=android.os.BinderProxy@3965242e} 08-23 22:56:14.587 25764-25764/com.testsample V/ActivityThread: Performing resume of ActivityRecord{22d326a9 token=android.os.BinderProxy@3965242e {com.testsample/com.testsample.MainActivity}} 08-23 22:56:14.604 25764-25764/com.testsample D/ActivityThread: ACT-AM_ON_RESUME_CALLED ActivityRecord{22d326a9 token=android.os.BinderProxy@3965242e {com.testsample/com.testsample.MainActivity}} 08-23 22:56:14.604 25764-25764/com.testsample V/ActivityThread: Resume ActivityRecord{22d326a9 token=android.os.BinderProxy@3965242e {com.testsample/com.testsample.MainActivity}} started activity: false, hideForNow: false, finished: false 08-23 22:56:14.604 25764-25764/com.testsample V/ActivityThread: Resuming ActivityRecord{22d326a9 token=android.os.BinderProxy@3965242e {com.testsample/com.testsample.MainActivity}} with isForward=true 08-23 22:56:14.604 25764-25764/com.testsample V/PhoneWindow: DecorView setVisiblity: visibility = 0 ,Parent =ViewRoot{339b80ea com.testsample/com.testsample.MainActivity,ident = 0}, this =com.android.internal.policy.impl.PhoneWindow$DecorView{2701db1d V.E..... R.....ID 0,0-1080,1920} 08-23 22:56:14.604 25764-25764/com.testsample V/ActivityThread: Scheduling idle handler for ActivityRecord{22d326a9 token=android.os.BinderProxy@3965242e {com.testsample/com.testsample.MainActivity}} 08-23 22:56:14.605 25764-25764/com.testsample D/ActivityThread: ACT-RESUME_ACTIVITY handled : 1 / android.os.BinderProxy@3965242e 08-23 22:56:14.621 25764-25790/com.testsample D/OpenGLRenderer: CanvasContext() 0xaaf50d08 initialize 0xab0a2f48 08-23 22:56:14.621 25764-25790/com.testsample D/Surface: Surface::connect(this=0xab0a2f40,api=1) 08-23 22:56:14.622 25764-25764/com.testsample V/InputMethodManager: onWindowFocus: null softInputMode=288 first=true flags=#81810100 08-23 22:56:14.622 25764-25790/com.testsample D/mali_winsys: new_window_surface returns 0x3000 08-23 22:56:14.622 25764-25764/com.testsample V/InputMethodManager: START INPUT: com.android.internal.policy.impl.PhoneWindow$DecorView{2701db1d V.E..... R.....ID 0,0-1080,1920} ic=null tba=android.view.inputmethod.EditorInfo@c6e6070 controlFlags=#104 08-23 22:56:14.622 25764-25790/com.testsample D/Surface: Surface::allocateBuffers(this=0xab0a2f40) 08-23 22:56:14.632 25764-25790/com.testsample D/Surface: Surface::setBuffersDimensions(this=0xab0a2f40,w=1080,h=1920) 08-23 22:56:14.633 25764-25790/com.testsample D/GraphicBuffer: register, handle(0xab0bd778) (w:1080 h:1920 s:1088 f:0x1 u:0x000b00)

Thanks In Advance

andy9775 commented 7 years ago

Where are you registering your press event listening function?

rimzici commented 7 years ago

In the index page inside componentWillMount() { Notification.addListener('press', function(e) { console.log(e); }); }

alse tried with DeviceEventEmitter none works finally i found what could be causing the issue, Not sure yet. I am using SplashScreen for my android app. Suspecting that SplashScreen Activity, one which takes place before my MainAcivity blocking the data? Thanks @andy9775

andy9775 commented 7 years ago

Hmm. Can you try registering in the constructor? I had a similar issue where it wasn't getting called and that solved it for me.

You could also be right about the splash screen activity I haven't experienced this type of scenario so I can't really comment on it though.

colorhaake commented 7 years ago

I have used react-native-gcm-android and react-native-system-notification.

Aftre research, the root cause could be: After react native v0.29, all native modules are migrated from MainActivity to MainApplication. Click notificaiton will send intent that contains notification info to launch App. We need to get this intent in app to get its notification info. But GcmModule is loaded before MainActivity launched because of migration of all native modules. So if we get this intent in MainApplication, that could be nothing. We need to get this intent in MainActivity and figure out way to send it into JavaScript side

If my thought is wrong, please point out.

dg92 commented 7 years ago

@rimzici i am using react-native-fcm, react-native-system-notification and need to generate push notification on background and foreground. Problem is i am using RN 0.32 and react-native-fcm that provide local-notification on RN > 0.33 So i tried to generate local notification by react-native-system-notification, now i am able to do that on fcm notification event writing react-native-system-notification oncreate event. componentDidMount() { FCM.on('notification', (notif) => { console.log('notif on foregournd', notif, FCM); Notification.create({ subject: 'Hey', message: 'Yo! Hello world.' }); }); but when click on it, it does not work. I have registered react-native-system-notification in willmount like this. componentWillMount() { this.setState({loadSpinner: true}, () => { // FCM.getFCMToken().then(token => { // this.props.dispatch(postPushNotificationDetails(token, DeviceInfo.getUniqueID())); // }); this.props.dispatch(fetchNewOrders(this.state.currentPage)); }); Notification.addListener('press', function(e) { console.log('Notification.addListener(press)'); }); }

Sh3rawi commented 7 years ago

The problem here is the following: The module here has two states only, if in foreground to the user, in which case there is no problem. And if the app is not in foreground (background, dead, service-only), in which the notification works only if the app is dead (completely or service-only). If the app is not foreground (from this module's point of view) then an intent to launch the application is sent along with the notification info. This would work only when the app is dead because of this line of code: https://github.com/neson/react-native-system-notification/blob/master/index.android.js#L67 The initial notification is checked when you add a listener to the press/click event only. Which means if the app is coming from the background, and the listener is already hooked, this piece of code won't run! Im trying to find the best solution for this problem as i write this comment. Will do a pull request once im done. But a viable temporary solution would be the following: listen to AppState changes, remove the listener when going to background and re-add it when you transition back to foreground.

componentDidMount() {
      Notification.addListener('press', this.notificationPress);
      AppState.addEventListener('change', this._handleAppStateChange);
}

 _handleAppStateChange(currentAppState) {
   if (currentAppState === 'background') {
     Notification.removeAllListeners('press');
   } else {
     Notification.addListener('press', this.notificationPress);
   }
 },
freakinruben commented 7 years ago

@Sh3rawi I've tried your solution with RN 0.39 but I still don't get any events when the notification is clicked when the app is in the background

Sh3rawi commented 7 years ago

@freakinruben, that worked for us at that time. Currently though, we do not use this library anymore. We switched to fcm from gcm, and react-native-fcm has built-in support for local notifications that covers our needs.