I'm to update the UI when onNotification arrives. In general onNotification works as expected when app is killed. It wakes up -> onNotification gets called.
BUT onNotification doesn't seem to be getting called when the app is NOT killed, i.e. in foreground or background.
I saw people reporting similar issues for local notifs, but I assume for remote notifications it's different?
Can someone suggest what could be wrong?
import React from 'react'
import { Platform } from 'react-native'
import PushNotification from 'react-native-push-notification'
import PushNotificationIOS from '@react-native-community/push-notification-ios'
const Root = () => {
PushNotification.configure({
onRegister: pushToken => {
console.log(pushToken.token)
},
onNotification: notification => {
console.log('Notification received', notification)
notification.finish(PushNotificationIOS.FetchResult.NoData)
},
onError: err => {
console.log('Error configuring notifications', err)
},
senderID: SENDER_ID,
permissions: {
alert: true,
badge: true,
sound: true,
},
popInitialNotification: true,
requestPermissions: Platform.OS === 'android',
})
return <Router />
}
The library version:
"react-native-push-notification": "^3.5.0",
Hi @alex-mironov,
Did you update the installation part of the library ?
In your code some properties are not used anymore (senderID) this is why you should check the configuration.
@Dallas62 thanks for the prompt reply. Yeah, sure, SENDER_ID is there, just dropped that import not to clutter the snippet.
I also updated native part
#import <RNCPushNotificationIOS.h>
...
// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];
}
// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
// Required for the notification event. You must call the completion handler after handling the remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
[RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
[RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
}
// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[RNCPushNotificationIOS didReceiveLocalNotification:notification];
}
As I mentioned, I was able to receive a plain notification when the app is killed, the only issue to make onNotification working when app is in the background/foreground.
Ok, since you mentioned iOS code, is it a bug on iOS or Android ?
If it's iOS, this is not an issue from this library
heh, yeah it's iOS...
@alex-mironov we also having this issue - we are able to receive and show the push notification when app is in background. However, when app is in the foreground, push notification is not received and shown.
We have also added the following in AppDelegate.m
//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}
Please let us know if you find a solution to your issue
Same in IOS onNotification is not working while app is in foreground and a message arrives. for time being we are using
import messaging from '@react-native-firebase/messaging'; to detect the message arrival
messaging().onMessage(async (remoteMessage) => {
console.log("onMessage", remoteMessage);
if (!remoteMessage) return;
if (CURRENT_OS === 'ios') return handleIosInteraction(remoteMessage);
return handleAndroidInteraction(remoteMessage);
})
but this does not address on notification tap issue while app is in foreground
@alex-mironov we also having this issue - we are able to receive and show the push notification when app is in background. However, when app is in the foreground, push notification is not received and shown.
We have also added the following in AppDelegate.m
//Called when a notification is delivered to a foreground app. -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge); }Please let us know if you find a solution to your issue
@snamstorm
I was also facing a similar issue. Not able to get this working in background for ios. Can you help me out to figure out what might be wrong?
I think I am also facing a similar issue in iOS.
The notification arrives. If I click on the notification the app comes to the foreground but the onNotification handler is not called.
In the XCode I see the log:
-[RNFirebaseNotifications sendJSEvent:name:body:] [Line 411] Multiple notification open events received before the JS Notifications module has been initialised
I call PushNotification.configure({ onNotification: handleNotificationClick }); from index.js so timing should not be the issue.
@alex-mironov do you see the same log in XCode? If not I guess I should open a new ticket ;)
@Dallas62 shouldn't onNotification in the NotificationHandler of this lib still fire?
I have things set up correctly I believe with push-notification-ios. When the app is in the background I receive the local scheduled notification just fine (so I assume my config with push-notification-ios is ok) but onNotification is never triggered.
@Dallas62 you were right. On the push-notification-ios#107 issue you eluded to the fix pointed out here enabled me to begin receiving onNotification events while the app was in the background or foreground. Thanks for your pointers!
Yes this fix also worked for me! Thanks for pointing it out again.
@alex-mironov @snamstorm I am now facing the opposite problem. onNotification is called correctly when coming from foreground or background but it is not called when the app was killed. The app just launches normally. Could you maybe let me know how you did the configuration?
I am calling PushNotification.configure from index.js so timing should not be the issue I guess. For Android it works correctly this way.
I will also create a new issue for it since this is not really related. Just wanted your feedback since it seems to be working for you ;)
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.INFO" />
</intent-filter>
</activity>
I am also facing similar issue in IOS.
When app is in foreground / background, onNotification handler is not getting called when notification is received, though this is working perfectly fine in android.
@nattri, Have you tried the adjustment I mentioned above?
I am also facing similar issue in IOS.
When app is in foreground / background,
onNotificationhandler is not getting called when notification is received, though this is working perfectly fine in android.
hi can you please help me on android notification to get the event in backgroud app.
Most helpful comment
@Dallas62 you were right. On the push-notification-ios#107 issue you eluded to the fix pointed out here enabled me to begin receiving onNotification events while the app was in the background or foreground. Thanks for your pointers!