UNUserNotificationCenter触发器未触发

时间:2017-02-02 21:10:12

标签: ios10 unusernotificationcenter

我正在尝试使用适用于iOS 10的UserNotifications框架。

如果我没有传入触发器,通知似乎效果很好。他们立即在应用程序中呈现。但我需要延迟触发器。任何人都能看到我的代码中的缺陷吗?它应该延迟15秒,但它永远不会消失。

UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"Hello!" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"Hello_message_body"
                                                     arguments:nil];
content.sound = [UNNotificationSound defaultSound];
content.categoryIdentifier = @"WhatUpsw";


NSString *imageName = @"hug2";

NSURL *url = [[NSBundle mainBundle]URLForResource:imageName withExtension:@"jpg"];


UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:imageName URL:url options:nil error:nil];
content.attachments = @[attachment];                          

// Deliver the notification in five seconds.
UNTimeIntervalNotificationTrigger* trigger = [     UNTimeIntervalNotificationTrigger
                                              triggerWithTimeInterval:15 repeats:NO];

UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecondsw"
                                                                      content:content trigger:trigger];
// Schedule the notification.
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
    if (error != nil) {
        NSLog(@"Something went wrong: %@",error);
    } else {
        NSLog(@"All good!: %@", request);
    }
}];

还检查待处理的请求,我看到:

<__NSArrayM 0x17024d170>(
<UNNotificationRequest: 0x1702235c0; identifier: FiveSecond, content:   <UNNotificationContent: 0x17010bd00; title: Hello!, subtitle: (null), body: Hello_message_body, categoryIdentifier: , launchImageName: , peopleIdentifiers: (
), threadIdentifier: , attachments: (
), badge: (null), sound: <UNNotificationSound: 0x1700b4580>, hasDefaultAction: YES, defaultActionTitle: (null), shouldAddToNotificationsList: YES, shouldAlwaysAlertWhileAppIsForeground: NO, shouldLockDevice: NO, shouldPauseMedia: NO, isSnoozeable: NO, fromSnooze: NO, darwinNotificationName: (null), darwinSnoozedNotificationName: (null), trigger: <UNTimeIntervalNotificationTrigger: 0x170223780; repeats: NO, timeInterval: 10.000000>>
)

3 个答案:

答案 0 :(得分:5)

只有当您的应用在前台运行时才会出现问题吗?如果在达到触发时间之前关闭应用程序或切换到另一个应用程序,通知是否正常工作?

为了在应用运行时显示通知,您需要在UNUserNotificationCenterDelegate中设置appController(_:didFinishLaunching:)

func appController(_ appController: TVApplicationController, didFinishLaunching options: [String : Any]?) {
  // ...

  UNUserNotificationCenter.current().delegate = self

  // ...
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification,
                              withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
  completionHandler(.alert)
}

需要在启动完成之前设置委托。

答案 1 :(得分:0)

我正在运行iOS 10.3 Beta和Xcode 8.3 beta 2,所以它可能只是一个错误。要解决此问题,我删除了内容扩展,重新启动了Xcode并再次添加了扩展程序。之后,每条消息都传来。

答案 2 :(得分:0)

最近2天,我在iOS 13上与这个问题作斗争。事实证明,我的通知标识符太长,或者因为我使用相同的字符串作为标题/消息的本地化字符串而不是“工作。

更改通知的ID,使它的长度为1.短,并且2.与其他任何都不相同,这解决了我的问题。