我正在尝试使用 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>>
)