【问题标题】:UserNotification is not showing (iOS 10)UserNotification 未显示(iOS 10)
【发布时间】:2017-01-21 22:11:50
【问题描述】:

我无法在 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 委托方法永远不会到达。

我错过了什么?

【问题讨论】:

  • 如果在前台启动一个应用,您不会在应用中看到自己的通知。

标签: ios objective-c usernotifications


【解决方案1】:

发现问题。

原来我需要设置内容的body,而不仅仅是title

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

我什至可以不用title 并且它可以工作。

【讨论】:

  • 谢谢,我想知道为什么我听到通知声音但没有看到任何警报...原来我只设置了subtitle,并且至少需要body的内容
  • 谢谢!足以将“”传递给身体。刚刚测试过
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多