-
Notifications
You must be signed in to change notification settings - Fork 281
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
Push notification in foreground iOS RN 0.60+ #63
Comments
what is the update ?? did you solve it ?? |
same issue |
I have working solution, I'll post it later today. |
Please note that
In the
Hope this helps |
Can we close this issue now or maybe we should add info above to the docs for people with the same issue? |
this solved my problem |
Version 1.0.6I got an error at:
|
Is there any way to conditionally hide remote notifications in the foreground (for instance if you are already looking at the chat thread that the message applies to)? I find that if I add willPresentNotification, notifications are shown in the foreground always and onNotification is only fired when the user clicks it. If I remove it, notifications are hidden in the foreground, but onNotification fires right away. I tried to send a local notification in this scenario, but of course it is not shown in the foreground either. |
If you would read carefully my message (#63 (comment)) you would find the example at the very bottom. |
Yes I saw that example-- your condition is some value in the notification payload. |
It's quite possible. When you want to block notification you need to save that information in the variable in the native side. When notification is received just read that variable and decide based on that. |
OK this is what I came up with based off of your help @dominiczaq . I don't know much on the native side, so this is hacky-- but it might help others looking to selectively block push notifications in the foreground:
|
I still can't get foreground notifications to work: When API sends a notificationRequest APN it does trigger a notification and localNotification also gets shown but only when app is in the background. I would like to show all notifications when app is foregrounded.
|
If you are using firebase and it is not working, you might wanna look at invertase/react-native-firebase#3367 |
@kyytiPetteri I followed every instruction to the T several times over, and still couldn't get it to work. After a day and a half of messing around, I right-clicked 🎉 Instant clarity! I've upgraded from RN 0.59 to 0.61.5 months ago, and then to 0.62.2 recently. React Native Firebase (which I only use for Android...) had auto-linked into my iOS build. I skipped auto-linking for the related packages, nuked and reinstalled my pods, and my foreground notifications now work as intended again. |
For those of you upgrading from version 1.1.1 to 1.2.0 please be aware that there was some breaking changes introduced (and not yet documented with how foreground notifications are handled. Specifically, before 1.2.0, native notifications would not be handled if app was in foreground and now they are. If you'd like to keep legacy behavior, I recommend changing your willPresentNotification as follows:
|
Hi, prior to implementing the additions in this page, I could not get |
It seems like if I comment out |
I thought I got it to work with @keithhackbarth code but now I have the same issue as @kovkev.
I agree, it's working without the
But I don't know if this can cause any issue |
I've also updated this library and followed the same steps of migration/installation. Now, getting push notifications when app is inactive or in background but not when app is in foreground. |
I have also tried all of these workarounds in this thread and several others and I am still not seeing onNotification triggered while the app is in the foreground/background. It is only triggered when the app is completely killed and a notification is selected. |
I set it up with the last version and this part work for me, when the app is in background/foreground and I click the notification, onNotification is triggered ! |
@thomas-negrault what do you mean the last version? Are you meaning the suggestion from @keithhackbarth and also removing the notification.finish line? Or are you talking about an actual specific version of push-notification-ios or react-native-push-notification? |
It seems some people in this thread have the issue after updating from an old version of this library to the last one, and that’s what causing the issue. In my case it’s a recent install from scratch. Maybe some change require by an old version of this library is now causing issues. |
@keithhackbarth's fix with a small change worked for me. I just gave it an empty completion handler to avoid the "There is no completion handler with notification id" error:
|
Thanks a lot @lowell-list-cambia it's working fine with your code ! |
I hope the |
I tried all of the solutions but i can't show the notifications in the foreground .
#import "AppDelegate.h" #import <React/RCTBridge.h> // Implement UNUserNotificationCenterDelegate to receive display notification via APNS for devices @implementation AppDelegate NSString *const kGCMMessageIDKey = @"gcm.message_id";
// Receive displayed notifications for iOS 10 devices.
// Required to register for notifications
// Required for the register event.
// [START receive_message]
// Required for the registrationError event.
@EnD |
I have the same error with v1.7.1.
|
@lowell-list-cambia For me, this error keeps popping up. Any idea what could cause this? In the JS part, i am using react-native-push-notifications onNotification: (notification) => {
// console.log("NOTIFICATION:", notification);
// process the notification
// call notification.finish
notification.finish(PushNotificationIOS.FetchResult.NoData);
}, AppDelegate.m
Error
|
These are my dependencies: react-native - 0.63.3 onNotification gets triggered and willPresentNotification get triggered. But, I still notification doesn't get showed when app is in foreground. In background everything works fine... |
Your answer helped me a lot. In my case, I was using
SOLVED THE PROBLEM. Now am getting the notification event fired. |
I follow ios basic installation guide as in the documentation here. For you with issue willPresentNotification not firing in AppDelegate.m, I follow this answer and works. If you use react-native-push-notification, using firebase messaging to send the notification, and using onNotification with this strategy, try to exclude gcm.message_id, fcm_options, and google.c.a.e field from userInfo when you want to show in foreground. function showNotification(notifId, data) {
let notifData = {
id: notifId,
...data,
}
if (Platform.OS === 'ios') {
/* iOS only properties */
const {
fcm_options: fcmOptions,
'gcm.message_id': gcmMessageId,
'google.c.a.e': googleCAE,
...otherUserInfoData
} = notifData.userInfo
notifData = {
...notifData,
userInfo: {
...otherUserInfoData,
},
}
}
PushNotification.localNotification(notifData)
}
PushNotification.configure({
onNotification(notification) {
if (notification.userInteraction) {
navigateFromNotification(notification.data)
} else if (notification.foreground) {
showNotification(notification.id, notification.data)
}
// (required) Called when a remote is received or opened, or local notification is opened
notification.finish(PushNotificationIOS.FetchResult.NoData)
},
} Background and quit state are working well in android and ios. Only foreground ios not showing.
|
It seems to be that we are all lost and we'll continue to be until this issue is well documented! Tested with: The solution:
Notes:
I'll be posting it on |
this just works great 💯 |
if I ever see you in person, I'll definitely give you a treat. |
I've spent weeks thinking what was wrong with my code even though I followed the examples thoroughly. I can't believe this is not documented yet. Thanks man! You saved me 👍 🎉 ! |
I would like to share a solution that I found with notifications in the foreground for iOS ... lines below my AppDelegate.m
#import "AppDelegate.h"
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <ReactNativeNavigation/ReactNativeNavigation.h>
#import <RNCPushNotificationIOS.h>
@import Firebase;
@implementation AppDelegate
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([FIRApp defaultApp] == nil) {
[FIRApp configure];
}
// Define UNUserNotificationCenter
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
NSURL *jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
[ReactNativeNavigation bootstrap:jsCodeLocation launchOptions:launchOptions];
return YES;
}
//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);
}
// 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];
}
(NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}
@EnD
The text was updated successfully, but these errors were encountered: