-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
onNotification not called when opening local notification in headless state (android) #1434
Comments
Hi @MarcoStb1993 |
Hi @MarcoStb1993 NotificationInterface.js
import PushNotification from 'react-native-push-notification';
import {AppState} from 'react-native';
import PushNotificationIOS from '@react-native-community/push-notification-ios';
export default class NotificationService {
constructor() {
this.configure(this.onRegister, this.onNotif);
this.lastId = 0;
}
static instance = null;
static getInstance() {
if (this.instance == null) this.instance = new NotificationService();
return this.instance;
}
onRegister = async token => {
console.log(token);
};
checkInitialNotification() {
PushNotification.popInitialNotification(this.onNotif);
}
onNotif = notif => {
if (!notif) {
return;
}
console.log('onNotif');
if (
notif.hasOwnProperty('data') &&
notif.data.hasOwnProperty('create_local') &&
notif.data.create_local
) {
console.log('remote notif received', notif);
if (!notif.foreground) {
this.localNotif(notif.data.text);
}
} else {
console.log('opened app with notif', notif);
if (Platform.OS === 'ios') {
PushNotificationIOS.setApplicationIconBadgeNumber(0);
}
}
if (Platform.OS === 'ios') {
notif.finish(PushNotificationIOS.FetchResult.NoData);
}
};
configure(onRegister, onNotification) {
console.log('configure');
PushNotification.configure({
onRegister: onRegister,
onNotification: onNotification,
permissions: {
alert: true,
badge: true,
sound: true,
},
popInitialNotification: true,
requestPermissions: true,
});
}
localNotif() {
this.lastId++;
PushNotification.localNotification({
/* Android Only Properties */
id: '' + this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
ticker: 'My Notification Ticker', // (optional)
autoCancel: true, // (optional) default: true
largeIcon: 'ic_launcher', // (optional) default: "ic_launcher"
smallIcon: 'ic_notification', // (optional) default: "ic_notification" with fallback for "ic_launcher"
bigText: 'My big text that will be shown when notification is expanded', // (optional) default: "message" prop
subText: 'This is a subText', // (optional) default: none
color: 'red', // (optional) default: system default
vibrate: true, // (optional) default: true
vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
tag: 'some_tag', // (optional) add tag to message
group: 'group', // (optional) add group to message
ongoing: false, // (optional) set whether this is an "ongoing" notification
/* iOS only properties */
alertAction: 'view', // (optional) default: view
category: '', // (optional) default: empty string
userInfo: {}, // (optional) default: {} (using null throws a JSON value '<null>' error)
/* iOS and Android properties */
title: 'Local Notification', // (optional)
message: 'My Notification Message', // (required)
playSound: false, // (optional) default: true
soundName: 'default', // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
number: 10, // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)
actions: '["Yes", "No"]', // (Android only) See the doc for notification actions to know more
});
}
scheduleNotif() {
this.lastId++;
PushNotification.localNotificationSchedule({
date: new Date(Date.now() + 3000), // in 3 secs
/* Android Only Properties */
id: '' + this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
ticker: 'My Notification Ticker', // (optional)
autoCancel: true, // (optional) default: true
largeIcon: 'ic_launcher', // (optional) default: "ic_launcher"
smallIcon: 'ic_notification', // (optional) default: "ic_notification" with fallback for "ic_launcher"
bigText: 'My big text that will be shown when notification is expanded', // (optional) default: "message" prop
subText: 'This is a subText', // (optional) default: none
color: 'blue', // (optional) default: system default
vibrate: true, // (optional) default: true
vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
tag: 'some_tag', // (optional) add tag to message
group: 'group', // (optional) add group to message
ongoing: false, // (optional) set whether this is an "ongoing" notification
/* iOS only properties */
alertAction: 'view', // (optional) default: view
category: '', // (optional) default: empty string
userInfo: {}, // (optional) default: {} (using null throws a JSON value '<null>' error)
/* iOS and Android properties */
title: 'Scheduled Notification', // (optional)
message: 'My Notification Message', // (required)
playSound: true, // (optional) default: true
soundName: 'default', // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
});
}
checkPermission(cbk) {
return PushNotification.checkPermissions(cbk);
}
requestPermissions() {
return PushNotification.requestPermissions();
}
cancelNotif() {
PushNotification.cancelLocalNotifications({id: '' + this.lastId});
}
cancelAll() {
PushNotification.cancelAllLocalNotifications();
}
} Then call Here changes:
checkInitialNotification() {
PushNotification.popInitialNotification(this.onNotif);
}
Hope it's clear enough 😉 |
Thank you for your help, unfortunately the method I am calling On a side note: The app's badge always sets to multiples of 10 after receiving a notification instead of 1 (10, 20, 30 ...). This happens on iOS as well. |
If the problem is now related to SplashScreen, take a look at: And I forgot to mention, but there is an extra condition in case of the app start normaly: if (!notif) {
return;
} |
For number badge, did you change the value in number: 10, |
I don't know if the issue is related to the splashscreen. I tried the "hack" from the comment you linked and it did not change the notification received being I removed the splashscreen intent from the manifest and set the And the badge number "error" was due to my stupidity ... Thanks for pointing the |
I will check this again at the end of the day, but I had the behaviour you described and making this change was enough to get the initial notification. |
Ok, thank you! I dug through the Receiving remote message05-14 09:45:59.408 25455 25502 V RNPushNotification: onMessageReceived: Bundle[{data=Bundle[{path=Settings, text=Remote notif, create_local=true}]}]
05-14 09:45:59.539 25455 25455 V RNPushNotification: sendNotification: Bundle[{userInteraction=false, id=1428018032, data=Bundle[{path=Settings, text=Remote notif, create_local=true}], foreground=false}]
05-14 09:45:59.549 25455 25455 D RNPushNotification: Cannot send to notification centre because there is no 'message' field in: Bundle[{userInteraction=false, id=1428018032, data=Bundle[{path=Settings, text=Remote notif, create_local=true}], foreground=false}] Local notification posted05-14 09:46:01.659 4664 5190 D Notification: allPendingIntents
05-14 09:46:01.661 5952 5952 D Notification.Badge: [valid] onNotificationPosted : StatusBarNotification(pkg=com.sampleapp user=UserHandle{0} id=1 tag=some_tag key=0|com.sampleapp|1|some_tag|10363: Notification(channel=rn-push-notification-channel-id-4-300 pri=1 contentView=null vibrate=null sound=null tick defaults=0x4 flags=0x10 color=0xffff0000 category=call groupKey=group actions=2 number=0 vis=PRIVATE semFlags=0x0 semPriority=0 semMissedCount=0))
05-14 09:46:01.661 5952 5952 D Notification.Badge: notificationKey : 0|com.sampleapp|1|some_tag|10363, count : 0, this.count : 1
05-14 09:46:01.661 5952 6042 D Notification.Badge: LauncherModel:onNotificationPosted() : com.sampleapp, count : [1], shouldBeFilteredOut : [false]
05-14 09:46:01.661 5952 6042 D Notification.Badge: notificationKey : 0|com.sampleapp|1|some_tag|10363, count : 1, mTotalCount : 1
05-14 09:46:01.661 4664 6935 D Notification: allPendingIntents
05-14 09:46:01.663 4664 5190 D Notification: allPendingIntents
05-14 09:46:01.663 5077 5077 D StatusBar: addNotification key=0|com.sampleapp|1|some_tag|10363 fullscreen:false
05-14 09:46:01.664 5952 6042 D Notification.Badge: updateLauncherIconBadges : 1
05-14 09:46:01.664 5952 6042 D Notification.Badge: updateLauncherIconBadges() count : -1
05-14 09:46:01.664 5952 6042 D Notification.Badge: updateLauncherIconBadges() item : [IconInfo(title=SampleApp intent=Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.sampleapp/com.zoontek.rnbootsplash.RNBootSplashActivity (has extras) } id=448 type=0 container=-102 screen=1 cellX=0 cellY=1 spanX=1 spanY=1 rank=4 hidden=0 dropPos=null user=UserHandle{0})], item.mBadgeCount: [1] Notification removed05-14 09:46:04.745 5952 5952 D Notification.Badge: [valid] onNotificationRemoved : StatusBarNotification(pkg=com.sampleapp user=UserHandle{0} id=1 tag=some_tag key=0|com.sampleapp|1|some_tag|10363: Notification(channel=rn-push-notification-channel-id-4-300 pri=1 contentView=null vibrate=null sound=null tick defaults=0x4 flags=0x10 color=0xffff0000 category=call groupKey=group actions=2 number=0 vis=PRIVATE semFlags=0x0 semPriority=0 semMissedCount=0))
05-14 09:46:04.745 5952 5952 D Notification.Badge: notificationKey : 0|com.sampleapp|1|some_tag|10363, count : 0, this.count : 1
05-14 09:46:04.745 5952 5952 D ViewRootImpl@3741015[LauncherActivity]: MSG_WINDOW_FOCUS_CHANGED 0
05-14 09:46:04.745 5952 6042 D Notification.Badge: LauncherModel:onNotificationRemoved() : com.sampleapp
05-14 09:46:04.745 5952 6042 D Notification.Badge: updateLauncherIconBadges : 1
05-14 09:46:04.745 5952 6042 D Notification.Badge: updateLauncherIconBadges() item : [IconInfo(title=SampleApp intent=Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.sampleapp/com.zoontek.rnbootsplash.RNBootSplashActivity (has extras) } id=448 type=0 container=-102 screen=1 cellX=0 cellY=1 spanX=1 spanY=1 rank=4 hidden=0 dropPos=null user=UserHandle{0})], item.mBadgeCount: [0] Furthermore, I've modified the 'getInitialNotification' method in the node module RNPushNotification.java@ReactMethod
public void getInitialNotification(Promise promise) {
Log.v(LOG_TAG, "getInitialNotification: called");
WritableMap params = Arguments.createMap();
Activity activity = getCurrentActivity();
if (activity != null) {
Log.v(LOG_TAG, "getInitialNotification: activity!=null");
Log.v(LOG_TAG, "getInitialNotification: activity: " + activity.getTitle());
Bundle bundle = this.getBundleFromIntent(activity.getIntent());
if (bundle != null) {
Log.v(LOG_TAG, "getInitialNotification: bunde!=null");
bundle.putBoolean("foreground", false);
String bundleString = mJsDelivery.convertJSON(bundle);
params.putString("dataJSON", bundleString);
}
}
promise.resolve(params);
} Which resulted in this being logged: Log on normal app start05-14 10:33:47.475 29051 29120 V RNPushNotification: getInitialNotification: called
05-14 10:33:47.475 29051 29120 V RNPushNotification: getInitialNotification: activity!=null
05-14 10:33:47.475 29051 29120 V RNPushNotification: getInitialNotification: activity: SampleApp Log on open local notification with app running headless5-14 10:45:02.161 31007 31007 D SoLoader: About to load: libyoga.so
05-14 10:45:02.162 31007 31007 D SoLoader: libyoga.so not found on /data/data/com.sampleapp/lib-main
05-14 10:45:02.162 31007 31007 D SoLoader: libyoga.so found on /data/app/com.sampleapp-Stgz-IKNP7kbnWG3Rr1Cpg==/lib/arm64
05-14 10:45:02.162 31007 31007 D SoLoader: Not resolving dependencies for libyoga.so
05-14 10:45:02.164 31007 31007 D SoLoader: Loaded: libyoga.so
05-14 10:33:59.656 29149 29209 V RNPushNotification: getInitialNotification: called
05-14 10:33:59.656 29149 29149 I zygote64: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
05-14 10:33:59.656 29149 29149 I zygote64: at void androidx.core.view.ViewCompat.setOnApplyWindowInsetsListener(android.view.View, androidx.core.view.OnApplyWindowInsetsListener) (ViewCompat.java:2421)
05-14 10:33:59.656 29149 29149 I zygote64: at android.view.ViewGroup androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:779)
05-14 10:33:59.656 29149 29149 I zygote64: at void androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:659)
05-14 10:33:59.656 29149 29149 I zygote64: at void androidx.appcompat.app.AppCompatDelegateImpl.setContentView(android.view.View) (AppCompatDelegateImpl.java:543)
05-14 10:33:59.656 29149 29149 I zygote64: at void androidx.appcompat.app.AppCompatActivity.setContentView(android.view.View) (AppCompatActivity.java:166)
05-14 10:33:59.656 29149 29149 I zygote64: at void com.facebook.react.ReactActivityDelegate.loadApp(java.lang.String) (ReactActivityDelegate.java:91)
05-14 10:33:59.656 29149 29149 I zygote64: at void com.facebook.react.ReactActivityDelegate.onCreate(android.os.Bundle) (ReactActivityDelegate.java:85)
05-14 10:33:59.656 29149 29149 I zygote64: at void com.facebook.react.ReactActivity.onCreate(android.os.Bundle) (ReactActivity.java:45)
05-14 10:33:59.656 29149 29149 I zygote64: at void com.sampleapp.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:33)
05-14 10:33:59.656 29149 29149 I zygote64: at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:7183)
05-14 10:33:59.656 29149 29149 I zygote64: at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1220)
05-14 10:33:59.656 29149 29149 I zygote64: at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2910)
05-14 10:33:59.657 29149 29149 I zygote64: at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:3032)
05-14 10:33:59.657 29149 29149 I zygote64: at void android.app.ActivityThread.-wrap11(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
05-14 10:33:59.657 29149 29149 I zygote64: at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1696)
05-14 10:33:59.657 29149 29149 I zygote64: at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:105)
05-14 10:33:59.657 29149 29149 I zygote64: at void android.os.Looper.loop() (Looper.java:164)
05-14 10:33:59.657 29149 29149 I zygote64: at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6944)
05-14 10:33:59.657 29149 29149 I zygote64: at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) (Method.java:-2)
05-14 10:33:59.657 29149 29149 I zygote64: at void com.android.internal.os.Zygote$MethodAndArgsCaller.run() (Zygote.java:327)
05-14 10:33:59.657 29149 29149 I zygote64: at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:1374)
05-14 10:33:59.657 29149 29149 I zygote64: Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList[[zip file "/data/app/com.sampleapp-KZ7upVvIpXkLCAdCw-hdLg==/base.apk"],nativeLibraryDirectories=[/data/app/com.sampleapp-KZ7upVvIpXkLCAdCw-hdLg==/lib/arm64, /data/app/com.sampleapp-KZ7upVvIpXkLCAdCw-hdLg==/base.apk!/lib/arm64-v8a, /system/lib64, /system/vendor/lib64]]
05-14 10:33:59.657 29149 29149 I zygote64: at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:93)
05-14 10:33:59.657 29149 29149 I zygote64: at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:379)
05-14 10:33:59.657 29149 29149 I zygote64: at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
05-14 10:33:59.657 29149 29149 I zygote64: at void androidx.core.view.ViewCompat.setOnApplyWindowInsetsListener(android.view.View, androidx.core.view.OnApplyWindowInsetsListener) (ViewCompat.java:2421)
05-14 10:33:59.657 29149 29149 I zygote64: at android.view.ViewGroup androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:779)
05-14 10:33:59.657 29149 29149 I zygote64: at void androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:659)
05-14 10:33:59.657 29149 29149 I zygote64: at void androidx.appcompat.app.AppCompatDelegateImpl.setContentView(android.view.View) (AppCompatDelegateImpl.java:543)
05-14 10:33:59.657 29149 29149 I zygote64: at void androidx.appcompat.app.AppCompatActivity.setContentView(android.view.View) (AppCompatActivity.java:166)
05-14 10:33:59.657 29149 29149 I zygote64: at void com.facebook.react.ReactActivityDelegate.loadApp(java.lang.String) (ReactActivityDelegate.java:91)
05-14 10:33:59.657 29149 29149 I zygote64: at void com.facebook.react.ReactActivityDelegate.onCreate(android.os.Bundle) (ReactActivityDelegate.java:85)
05-14 10:33:59.657 29149 29149 I zygote64: at void com.facebook.react.ReactActivity.onCreate(android.os.Bundle) (ReactActivity.java:45)
05-14 10:33:59.657 29149 29149 I zygote64: at void com.sampleapp.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:33)
05-14 10:33:59.657 29149 29149 I zygote64: at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:7183)
05-14 10:33:59.657 29149 29149 I zygote64: at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1220)
05-14 10:33:59.657 29149 29149 I zygote64: at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2910)
05-14 10:33:59.657 29149 29149 I zygote64: at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:3032)
05-14 10:33:59.657 29149 29149 I zygote64: at void android.app.ActivityThread.-wrap11(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
05-14 10:33:59.657 29149 29149 I zygote64: at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1696)
05-14 10:33:59.657 29149 29149 I zygote64: at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:105)
05-14 10:33:59.657 29149 29149 I zygote64: at void android.os.Looper.loop() (Looper.java:164)
05-14 10:33:59.657 29149 29149 I zygote64: at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6944)
05-14 10:33:59.657 29149 29149 I zygote64: at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) (Method.java:-2)
05-14 10:33:59.657 29149 29149 I zygote64: at void com.android.internal.os.Zygote$MethodAndArgsCaller.run() (Zygote.java:327)
05-14 10:33:59.657 29149 29149 I zygote64: at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:1374) Hopefully, this might be helpful. Edit: I found this regarding splash screens in RNF's issues. which shows the same problem in RNF with |
There is a difference in the AndroidManifest I used and your AndroidManifest: |
I've added this tag both my activities but it still did not work. But, by chance I found out, that the initial notification is fetched when not in Debug mode. As soon as I activate the Debug mode, I receive Do you have any idea why that can be? |
I don’t know what it can be, debug can replay the same initial notification (null if no notification) but its due to Hot Reload. |
I tried to build a small repository for testing just using your example project and adding RN Bootsplash, but using just this combination works as expected. I guess its some misconfiguration in my project using submodules and tons of dependencies ... I will close this issue now since I can proceed (without debug mode). Thanks again for your help! |
Hi @MarcoStb1993 |
The issue is due to splash activity
|
happens to me also, completely removing FYI there is instructions to add separate activity for Splash like this
but the README of https://github.com/crazycodeboy/react-native-splash-screen |
@MarcoStb1993 Have you been able to resolve your issues? Asking as I'm experiencing the same symptoms in one of my projects. |
@Draigs Unfortunately, I can't really tell. It seemed to work for some tests back then, but was never really used in a productive app until recently when it did not work. Currently I am working on another project, but soon I'll try to fix it. But I reckon since a lot of new releases happened since then, it will be quite different. |
@MarcoStb1993 thanks for getting back. Finally found my problem. Was a misconfiguration of the manifest. As I'd implemented SSO, I had to change the intent-filter:
If I changed this, to what's written in the documentation of this repo, the notification were working but obviously it broke the SSO flow. Finally I fixed it by adding an additional intent-filter and left the existing one(s) unchanged:
Maybe this helps someone having similar issues. |
Bug
I want to handle remote push notifications (PN) from FCM in my app by sending silent remote PNs that trigger a local PN (as advised in this package's troubleshooting guide). This works for all states but one: When the app is in quit state and receives a remote PN, the background task is launched,
onNotification
called and a local PN is created (PushNotification.configure
is called as well). When this local PN is opened, theonNotification
method is not called. If the app is in background state and receives a remote PN, the local PN is created andonNotification
called as expected when it is opened.Environment info
react-native info
output:Library version: 3.4.0
Steps To Reproduce
Describe what you expected to happen:
onNotification
is called with respective log output ("remote notif received")onNotification
is called with log output "opened app with notif" (which unfortunately is not happening)Log output for remote PN received in app background state and opening local PN:
log
Log output for remote PN received in app quit state on spinning up background task and opening local PN:
log
Reproducible sample code
index.js
NotificationInterface.js
AndroidManifest.xml
The text was updated successfully, but these errors were encountered: