未显示UserNotification(iOS 10) [英] UserNotification is not showing (iOS 10)

查看:95
本文介绍了未显示UserNotification(iOS 10)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

然后,我有:

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

但是我在屏幕上看不到任何通知(触发延迟只有1秒,但是我也尝试了更大的值).

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

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

Also, the willPresentNotification delegate method is never reached.

我想念什么?

推荐答案

发现了问题.

事实证明,我需要设置内容的body,而不仅仅是title:

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];

我什至可以省去title,并且可以使用.

I can even leave the title out and it works.

这篇关于未显示UserNotification(iOS 10)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆