当应用程序处于前台时,即使使用UNNotificationPresentationOptionAlert,也不会显示通知警报

时间:2017-05-13 23:26:10

标签: ios objective-c push-notification apple-push-notifications ios10

在ios 10+中,在AppDelegate.h文件中,我导入UserNotifications并设置UNUserNotificationCenterDelegate,如下所示:

#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate>

然后在AppDelegate.m文件中,我在[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];方法中添加didFinishLaunchingWithOptions

然后我添加userNotificationCenterhandler,如下所示。

- (void)userNotificationCenter:(UNUserNotificationCenter* )center willPresentNotification:(UNNotification* )notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {

    completionHandler(UNNotificationPresentationOptionAlert);

  }

但是当应用程序处于前台时,没有显示警告:(我错过了什么?

1 个答案:

答案 0 :(得分:0)

Apple说: 如果您的应用在前台运行时收到本地或远程通知,则您有责任以特定于应用的方式将信息传递给用户。 当app在前台notification delegate时被调用。您需要在

中实施警报
application:didReceiveRemoteNotification:fetchCompletionHandler: delegate