UNUserNotificationCenter触发器未触发

问题描述 投票:2回答:3

我正在尝试使用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>>
)
ios10 unusernotificationcenter
3个回答
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)
}

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


0
投票

我正在运行iOS 10.3 Beta和Xcode 8.3 beta 2,因此可能只是一个错误。为了解决此问题,我删除了内容扩展名,重新启动Xcode并再次添加了扩展名。之后,每条消息都通过了。


0
投票

我花了最后两天的时间在iOS 13上解决此问题。事实证明,我的通知标识符太长,或者因为我使用相同的字符串作为标题/消息的本地化字符串,而不是工作。

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

© www.soinside.com 2019 - 2024. All rights reserved.