react-native-push-notification icon indicating copy to clipboard operation
react-native-push-notification copied to clipboard

iOS local notification is not shown if the app is foreground

Open abeikverdi opened this issue 10 years ago • 41 comments

In iOS when my app is in foreground and opened the localnotification is not displayed.

abeikverdi avatar Nov 10 '16 06:11 abeikverdi

Yes, I think this is expected. If you use the react-native push notification module do you see the same behaviour?

npomfret avatar Nov 10 '16 11:11 npomfret

Why shouldnt I have to option of displaying something in the notification center on iphone? Like lets say some process is running and I want to keep the notification in the notification center as long as the process is running on the phone even though the app is closed by the user. Now I am not able to display the notification if the apps in foreground.

abeikverdi avatar Nov 11 '16 08:11 abeikverdi

Well if this is the default behaviour its something you should take up with apple. This project really only adds android support for push notifications to react-native.

If you're only interested in an iOS implementation there's no point using this library at all.

npomfret avatar Nov 11 '16 11:11 npomfret

I think this issue can closed. This is a feature of Apple and not a bug. I think apps that has this functionality, made it by themself.

vdlindenmark avatar Mar 08 '17 21:03 vdlindenmark

It's now possible to display local notifications while the app is in foreground in iOS10: http://stackoverflow.com/questions/39713605/getting-local-notifications-to-show-while-app-is-in-foreground-swift-3. Would really appreciate it if this could be implemented.

ylac avatar May 18 '17 06:05 ylac

Bump @zo0r - This behaviour is now seen in many major iOS apps and it would be great if we could do it too with this plugin.

nabilfreeman avatar Jul 11 '17 10:07 nabilfreeman

@ylac @nabilfreeman Did you found any way to do this via this plugin ?

Help needed ! Anyone ?

jitinmaher avatar Aug 16 '17 12:08 jitinmaher

I figured out how to get this to work. I still use this package to register the Push Notification The only thing I added is implement UNUserNotificationCenter in my AppDelegate.m. The only delegate I need is userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler to show the notification when app is in the foreground. This might not be the best implementation but it's working for me so far. Please let me know if y'all find a better solution.

chuongle avatar Sep 15 '17 21:09 chuongle

@chuongle, do you have an example of your AppDelegate.m? I didn't understand that file so don't know where to put the code.

I think the code has to be added somewhere in the code below (a part of my AppDelegate.m)

// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
  [RCTPushNotificationManager didRegisterUserNotificationSettings:notificationSettings];
}
// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  [RCTPushNotificationManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
  [RCTPushNotificationManager didFailToRegisterForRemoteNotificationsWithError:error];
}

// Required for the notification event.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
  [RCTPushNotificationManager didReceiveRemoteNotification:notification];
}
// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
  [RCTPushNotificationManager didReceiveLocalNotification:notification];
}

// Required for deeplinking
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
  return [RCTLinkingManager application:application openURL:url
                      sourceApplication:sourceApplication annotation:annotation];
}

vdlindenmark avatar Sep 27 '17 21:09 vdlindenmark

@vdlindenmark Sorry for the late response. This is what I had for my AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // some other codes generate by RN
  // ...

  // define UNUserNotificationCenter
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  center.delegate = self;

  return YES;
}


// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
  [RCTPushNotificationManager didRegisterUserNotificationSettings:notificationSettings];
}
// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  [RCTPushNotificationManager 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
{
  [RCTPushNotificationManager didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
  [RCTPushNotificationManager didFailToRegisterForRemoteNotificationsWithError:error];
}
// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
  [RCTPushNotificationManager didReceiveLocalNotification:notification];
}

//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  NSLog(@"User Info : %@",notification.request.content.userInfo);
  completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}

Please let me know if this works for you or if you found a better solution. Thanks!

chuongle avatar Oct 03 '17 16:10 chuongle

@chuongle it works well, the only think that need to add to the AppDelegate is the import:

#import <UserNotifications/UserNotifications.h>

i think that u should pull your snippet of code to PushNotificationIOS repo. Thank's at all

7laria avatar Oct 05 '17 09:10 7laria

@7laria that's very helpful!

I got it working, only now I notice that PushNotification.configure ({onNotification .... does not work anymore. Without your code, everything works properly.

vdlindenmark avatar Oct 09 '17 18:10 vdlindenmark

@chuongle Thanks! This works for me as well but as @vdlindenmark this also means that onNofication is not triggered anymore.

I know nothing about iOS development but I tried to dig around a bit. react-native-fcm also works with notifications. As far as I can tell their changes to AppDelegate.m involve calling their own native code RNFIRMessaging willPresentNotification:notification and the completionHandler. The changes above only runs the completion handler.

I tried also calling the notification manager but that does not work. The app crashes when receiving a notification.

//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  [RCTPushNotificationManager didReceiveLocalNotification:notification];
  completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}


kasperg avatar Oct 12 '17 15:10 kasperg

If you use react-native-fcmor react-native-firebase you should rely on those module to read notification. This module cannot work correcly with one of those 2 (onNotification is never triggered).

MoOx avatar Nov 24 '17 15:11 MoOx

Thanks @chuongle your solution works 👍

I wish it get applied soon.

ikhattab avatar May 14 '18 11:05 ikhattab

@vdlindenmark @kasperg I got it to fire onNotification by changing it to:

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
  NSLog(@"User Info : %@",notification.request.content.userInfo);
  [RCTPushNotificationManager didReceiveRemoteNotification:notification.request.content.userInfo fetchCompletionHandler:completionHandler];
  completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}

I don't know any native code and figured this out from just reading the build errors, but initial testing seems fine...

It refires onNotification on press with a new notificationId, however, userInteraction remains false. I use that for app routing (userInteraction true) so I'll have to come up with a way of getting around that.

Looking at the package's code:

this.onNotification({
	foreground: ! isFromBackground,
	userInteraction: isFromBackground,
	message: data.getMessage(),
	data: data.getData(),
	badge: data.getBadgeCount(),
	alert: data.getAlert(),
	sound: data.getSound(),
	finish: (res) => data.finish(res)
});

It would require a change since userInteraction is only true if isFromBackground...

EthanDM avatar Jun 12 '18 22:06 EthanDM

@chuongle thank you!!!

byennen avatar Jun 18 '18 23:06 byennen

@chuongle it works. U save my day. Thanks!

thambt avatar Sep 10 '18 02:09 thambt

@chuongle it works perfectly but how do we get rid of the notification from showing in foreground. I want to handle it my own way (i.e display my own custom made view) and not the default notification drop down in the app. I basically just want the onNotification callback to be triggered without the default ios alert dropdown. I'll appreciate any help I can get, thanks

Osebrian avatar Oct 12 '18 09:10 Osebrian

Unfortunately @EthanDM's solution doesn't seem to work anymore, or at least for me. I still have no notification with the app in foreground.

Anyone else has a working solution?

zedtux avatar Oct 12 '18 15:10 zedtux