UserNotification is not showing (iOS 10)

萝らか妹 提交于 2019-12-20 04:24:24

问题


I am unable to fire a UserNotification in iOS 10. I will start with my code:

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

//

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        UNAuthorizationOptions options = (UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound);

        center.delegate = self;

        [center requestAuthorizationWithOptions: options
                              completionHandler: ^(BOOL granted, NSError * _Nullable error) {
                                  if (granted) {
                                      NSLog(@"Granted notifications!");
                                  }
                              }];
//

This works, I see the log.

Then, I have:

-(void)application: (UIApplication *)application performFetchWithCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandler {
 //

UNMutableNotificationContent *content = [UNMutableNotificationContent new];
                content.title = NSLocalizedString(@"Updates are available", nil);

                UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval: 1
                                                                                                                repeats: NO];
                UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier: @"IDLocalNotification"
                                                                                      content: content
                                                                                      trigger: trigger];

                UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

                [center addNotificationRequest: request withCompletionHandler: ^(NSError * _Nullable error) {
                    if (!error) {
                        NSLog(@"Notification was requested!");
                    }
                }];

Again, this works, I see the log.

But I don't see a notification appear on my screen (the trigger delay is only 1 sec, but I tried larger values as well).

Also, the willPresentNotification delegate method is never reached.

What am I missing?


回答1:


Found the problem.

It turns out that I need to set the body of the content, not just the title:

UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.body = [NSString localizedUserNotificationStringForKey: @"Updates are available" arguments: nil];

I can even leave the title out and it works.



来源:https://stackoverflow.com/questions/41785105/usernotification-is-not-showing-ios-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!