UserNotification未显示(iOS 10)

时间:2017-01-21 22:11:50

标签: ios objective-c usernotifications

我无法在iOS 10中触发UserNotification。我将从我的代码开始:

- (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!");
                                  }
                              }];
//

这很有效,我看到了日志。

然后,我有:

-(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!");
                    }
                }];

再次,这是有效的,我看到了日志。

但我没有看到屏幕上出现通知(触发延迟仅为1秒,但我也尝试了更大的值)。

此外,永远不会达到willPresentNotification委托方法。

我错过了什么?

1 个答案:

答案 0 :(得分:3)

发现问题。

事实证明,我需要设置内容的.first .second { display: none; } .second { display: block; } ,而不仅仅是body

title

我甚至可以将UNMutableNotificationContent *content = [UNMutableNotificationContent new]; content.body = [NSString localizedUserNotificationStringForKey: @"Updates are available" arguments: nil]; 退出并且有效。