首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UNUserNotificationCenter触发器未触发

UNUserNotificationCenter触发器未触发
EN

Stack Overflow用户
提问于 2017-02-03 05:10:12
回答 3查看 2.9K关注 0票数 3

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

如果我不传入触发器,通知似乎工作得很好。它们会立即出现在app中。但我需要传递触发的延迟时间。有人能看到我代码中的缺陷吗?它应该让它延迟15秒,但它从来没有响过。

代码语言:javascript
复制
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);
    }
}];

我还看到检查挂起的请求:

代码语言:javascript
复制
<__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>>
)
EN

回答 3

Stack Overflow用户

发布于 2017-08-19 02:35:44

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

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

代码语言:javascript
复制
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)
}

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

票数 5
EN

Stack Overflow用户

发布于 2020-03-18 10:17:33

我花了两天的时间在iOS 13上解决这个问题。结果发现我的通知标识符要么太长,要么因为我使用相同的字符串作为标题/消息的本地化字符串,它不起作用。

更改通知的ID,使其为1.短,2.与其他任何东西不同,解决了我的问题。

票数 1
EN

Stack Overflow用户

发布于 2017-02-10 01:01:07

我运行的是iOS 10.3Beta和Xcode8.3Beta2,所以这可能只是一个bug。为了解决这个问题,我删除了内容扩展,重新启动了Xcode,并再次添加了扩展。在那之后,每一条信息都传来了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42012351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档