zo0r / react-native-push-notification

React Native Local and Remote Notifications
MIT License
6.75k stars 2.05k forks source link

iOS local notification is not shown if the app is foreground #275

Closed abeikverdi closed 2 years ago

abeikverdi commented 7 years ago

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

npomfret commented 7 years ago

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

abeikverdi commented 7 years ago

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.

npomfret commented 7 years ago

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.

vdlindenmark commented 7 years ago

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.

ylac commented 7 years ago

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.

nabilfreeman commented 7 years ago

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.

jitinmaher commented 7 years ago

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

Help needed ! Anyone ?

chuongle commented 6 years ago

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.

vdlindenmark commented 6 years ago

@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];
}
chuongle commented 6 years ago

@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!

7laria commented 6 years ago

@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

vdlindenmark commented 6 years ago

@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.

kasperg commented 6 years ago

@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);
}
MoOx commented 6 years ago

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).

ikhattab commented 6 years ago

Thanks @chuongle your solution works 👍

I wish it get applied soon.

EthanDM commented 6 years ago

@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...

byennen commented 6 years ago

@chuongle thank you!!!

thambt commented 6 years ago

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

Osebrian commented 5 years ago

@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

zedtux commented 5 years ago

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?

Osebrian commented 5 years ago

For me, the problem was as a result of trying to use react-native-firebase while react-native-onesignal was already installed, so there was conflict as react-native-onesignal was trying to handle notifications as well as firebase. Once I installed it, everything worked perfectly as expected. Hope this helps.

HanKyongIl commented 5 years ago

@chuongle Thank you very much!

jacquesdev commented 5 years ago

As part of @chuongle's answer, just to note the information about AppDelegate.h seems to be missing (I had never worked on native code before so I had no idea, but it is mentioned in this issue https://github.com/zo0r/react-native-push-notification/issues/919, so just thought I'd link it).

Arama2014 commented 5 years ago

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).

Any idea what that would look like code wise for AppDelegate.m

1b0t commented 5 years ago

@EthanDM your workaround (which works for now 🎉) potentially calls the completionHandler twice which could cause all kinds of weird issues.

It would be best to officially support this. However, as far as I can tell the right place to implement this would be the official react-native lib PushNotificationIOS - not this project.

taminhtien commented 5 years ago

@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...

Hi, do you have solution for this? I have the same case with you. userInteraction is always false so I cannot do routing.

EthanDM commented 5 years ago

Hi, do you have solution for this? I have the same case with you. userInteraction is always false so I cannot do routing.

No, I haven't looked into it much since my earlier comment. I resorted to using a custom RN component implementation that slides down from the top of the screen (think something like Instagram).

The reason I abandoned this is because I realized I need to conditionally show/hide certain notifications depending on what screen the user is on. For example, if you are in a chat and receiving messages you don't want notifications to show. I wasn't sure how to stop that over the bridge, but in RN it's easy to conditionally show a custom component based on something in the notification payload like the chat id.

asleepace commented 5 years ago

I have a slightly hackish solution that builds on the previous answers, put this in the app delegate:

// 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);
}

// Called when a user taps on a notification in the foreground
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
    NSMutableDictionary *userData = [NSMutableDictionary dictionaryWithDictionary:response.notification.request.content.userInfo];
    [userData setObject:@(1) forKey:@"openedInForeground"];
    [RCTPushNotificationManager didReceiveRemoteNotification:userData];
    completionHandler();
}

and then in my onNotification method:

  onNotification(notification) {
    console.log('[Notifications] on notification:', notification);

    // Check if opened from forground
    if (notification.data.openedInForeground) {
        notification.userInteraction = true;
    }

    if (notification.userInteraction)
    {
      this.onNotificationCallback?.(notification);
    }

    // Only call callback if not from foreground
    if (!notification.data.openedInForeground) {
      notification.finish(PushNotificationIOS.FetchResult.NoData);
    }
  }
taminhtien commented 5 years ago

I have a slightly hackish solution that builds on the previous answers, put this in the app delegate:

// 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);
}

// Called when a user taps on a notification in the foreground
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
    NSMutableDictionary *userData = [NSMutableDictionary dictionaryWithDictionary:response.notification.request.content.userInfo];
    [userData setObject:@(1) forKey:@"openedInForeground"];
    [RCTPushNotificationManager didReceiveRemoteNotification:userData];
    completionHandler();
}

and then in my onNotification method:

  onNotification(notification) {
    console.log('[Notifications] on notification:', notification);

    // Check if opened from forground
    if (notification.data.openedInForeground) {
        notification.userInteraction = true;
    }

    if (notification.userInteraction)
    {
      this.onNotificationCallback?.(notification);
    }

    // Only call callback if not from foreground
    if (!notification.data.openedInForeground) {
      notification.finish(PushNotificationIOS.FetchResult.NoData);
    }
  }

It works. Thank you!

moseslucas commented 5 years ago

@chuongle and @7laria works for me

wzulfikar commented 4 years ago

Thanks @chuongle, it works :)

At first it didn't work. But then I realised that I forgot to define UNUserNotificationCenter in didFinishLaunchingWithOptions (like what you put in example).

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

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

  return YES;
}
hareshgediya commented 4 years ago

It works for me 👍

AppDelegate.h

#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate>

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // ...

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

  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);
}
programmer-RN commented 4 years ago

It works for me 👍

AppDelegate.h

#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate>

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // ...

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

  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);
}

Hi, my AppDelegate.h and AppDelegate.m have code same as yours, but still cannot display notification in app. May I know how you solve this problem? @HareshGediya

belal-mazlom commented 4 years ago

I think changes mentioned by @HareshGediya and @wzulfikar should be added to setup of plugin I spend hours trying to figure out why local notification not appear in foreground ...

Abhishek-Sankey commented 4 years ago

i've also the same implementation but "willPresentNotification" method is not getting called. I tried to do NSLogs in that method but nothing gets printed on the console.

khaledBou commented 4 years ago

I my AppDelegate.h : #import <UserNotifications/UNUserNotificationCenter.h> @interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate>

In my AppDeledate.m :

`/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#import "AppDelegate.h"

#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <UserNotifications/UserNotifications.h>
#import <RNCPushNotificationIOS.h>

@import UserNotifications;

// Implement UNUserNotificationCenterDelegate to receive display notification via APNS for devices
// running iOS 10 and above.
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@end

@implementation AppDelegate

NSString *const kGCMMessageIDKey = @"gcm.message_id";

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // Use Firebase library to configure APIs
  // [START configure_firebase]
  [FIRApp configure];
  // [END configure_firebase]

  // [START set_messaging_delegate]
  [FIRMessaging messaging].delegate = self;
  // [END set_messaging_delegate]

  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@"eXpanded"
                                            initialProperties:nil];

  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];

  // ####################################
  if ([UNUserNotificationCenter class] != nil) {
  // iOS 10 or later
  // For iOS 10 display notification (sent via APNS)
  [UNUserNotificationCenter currentNotificationCenter].delegate = self;
  UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
      UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
  [[UNUserNotificationCenter currentNotificationCenter]
      requestAuthorizationWithOptions:authOptions
      completionHandler:^(BOOL granted, NSError * _Nullable error) {
        // ...
      }];
  } else {
    // iOS 10 notifications aren't available; fall back to iOS 8-9 notifications.
    UIUserNotificationType allNotificationTypes =
    (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
    UIUserNotificationSettings *settings =
    [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
    [application registerUserNotificationSettings:settings];
  }

  [application registerForRemoteNotifications];
  // ####################################

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

  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);
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
  return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}

// 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];
}
// IOS 10+ Required for localNotification event
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)(void))completionHandler
{
  [RNCPushNotificationIOS didReceiveNotificationResponse:response];
  completionHandler();
}
// IOS 4-10 Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
 [RNCPushNotificationIOS didReceiveLocalNotification:notification];
}

@end
`

I can't show notifications in foreground . Any suggestions ? and what about RCTPushNotificationManager how can i implement it ?

Nadreasky commented 3 years ago

I have added all the code above into my AppDelegate.m but foreground notification still not show.

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

the log is shown prove that the function is triggered but the noti does not show up. Anyone can help me ?

lvmtam commented 3 years ago

@Nadreasky I'd a same issue :(

parulgarg004 commented 3 years ago

I have added all the code above into my AppDelegate.m but foreground notification still not show.

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

the log is shown prove that the function is triggered but the noti does not show up. Anyone can help me ?

I'm facing the same issue, notification is received as per the log but not visible on device.

parulgarg004 commented 3 years ago

I have added all the code above into my AppDelegate.m but foreground notification still not show.

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

the log is shown prove that the function is triggered but the noti does not show up. Anyone can help me ?

I'm facing the same issue, notification is received as per the log but not visible on device.

Solved this by moving the delegate inside the completion handler of permissions, notification is visible in the foreground as well now.

 if ([UNUserNotificationCenter class] != nil) {
    // iOS 10 or later
    // For iOS 10 display notification (sent via APNS)
    UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
        UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
    [[UNUserNotificationCenter currentNotificationCenter]
        requestAuthorizationWithOptions:authOptions
        completionHandler:^(BOOL granted, NSError * _Nullable error) {
          // ...
            [UNUserNotificationCenter currentNotificationCenter].delegate = self;
        }];
  }
github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions.