gpt4 book ai didi

ios10 - UNUserNotificationCenter 触发器未触发

转载 作者:行者123 更新时间:2023-12-04 08:11:44 26 4
gpt4 key购买 nike

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

最佳答案

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

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

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

委托(delegate)需要在启动完成前设置。

关于ios10 - UNUserNotificationCenter 触发器未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42012351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com