UNUserNotificationCenter Swift - 在特定情况下不会触发本地通知

2023-12-24

我在应用程序中使用本地通知时遇到问题。

我正在安排带有自定义内容和自定义操作类别的本地通知。它在 iOS 11 中进行调度和触发,但在 iOS 10 中则不然。

根据文档,它应该也可以在 iOS 10 中运行。

为什么我的代码在这种特定情况下不起作用?

代码如下:

AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    .......

    if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().delegate = self
    }
    .......

    return true
}

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

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    
    print("Local Notification :: ", response)
}

自定义操作代码

func configureNotificationCenter() {

    let actionRemindAgain = UNNotificationAction(identifier: NTConstants.LocalNotification.action.remindMeAgain, title: "Remind me Again!⏰", options: [.authenticationRequired])
    let actionViewDetails = UNNotificationAction(identifier: NTConstants.LocalNotification.action.viewDetails, title: "View Details!", options: [.foreground])
    let actionClose = UNNotificationAction(identifier: NTConstants.LocalNotification.action.dismiss, title: "Dismiss", options: [])
    
    //Event Category
    let eventCategory = UNNotificationCategory(identifier: NTConstants.LocalNotification.category.reminder, actions: [actionRemindAgain, actionViewDetails, actionClose], intentIdentifiers: [], options: [])
    
    //Register Category
    UNUserNotificationCenter.current().setNotificationCategories([eventCategory])
}

时间表代码

func scheduleLocalNotification() {
    
    self.configureNotificationCenter()
    
    //Local Notification
    let content = UNMutableNotificationContent()
    content.title = notifyTitle
    content.subtitle = notifySubTitle
    content.body = notifyDesc
    
    //Category
    content.categoryIdentifier = "reminder"
    
    //Trigger
    let scheduleDate = Date(timeIntervalSince1970: TimeInterval(scheduleTimeInterval / 1000)) //My Schedule Data
    let triggerDate = Calendar.current.date(byAdding: .hour, value: -2, to: scheduleDate) // Should be call before 2 hours
    let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: triggerDate!)
    
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
    
    //Request
    let request = UNNotificationRequest(identifier: "notification_\((notifyId))", content: content, trigger: trigger)
    
    //Add Request
    UNUserNotificationCenter.current().add(request) { (error) in
        if let error = error {
            NTPrint("Unable to add notification : \(error.localizedDescription)")
        }
        else {
            NTPrint("Scheduled :: ", request.identifier)
        }
    }
}

此代码成功运行并安排本地通知。结果如下:

<UNNotificationRequest: 0x18d65360; 

identifier: notification_111, 

content: <UNNotificationContent: 0x18daead0; 

    title: Event..!!, 
    subtitle: 31 Mar at 2:00 PM to 01 Apr at 12:00 PM, 
    body: , 
    categoryIdentifier: reminder, 
    launchImageName: , 
    peopleIdentifiers: (), 
    threadIdentifier: , 
    attachments: (), 
    badge: (null), 
    sound: (null), 
    hasDefaultAction: YES, 
    defaultActionTitle: (null), 
    shouldAddToNotificationsList: YES, 
    shouldAlwaysAlertWhileAppIsForeground: NO, 
    shouldLockDevice: NO, 
    shouldPauseMedia: NO, 
    isSnoozeable: NO, 
    fromSnooze: NO, 
    darwinNotificationName: (null), 
    darwinSnoozedNotificationName: (null), 

    trigger: <UNCalendarNotificationTrigger: 0x18d51470; 
        dateComponents: <NSDateComponents: 0x18da3320>
        Calendar Year: 2018
        Month: 3
        Leap month: no
        Day: 31
        Hour: 12
        Minute: 00, 
    repeats: NO
>>

中午 12 点设置正确,因为我在 2 小时前设置。它在 iOS 11 设备上运行良好。但在 iOS 10 中不起作用。更新后的代码有什么问题吗?或者还有什么需要处理的吗?


要生成本地通知,内容.body不得为空或空白。

万一在解析时blank就正文而言,iOS 10不会生成本地推送。但它可以在 iOS 11 中运行。

的价值内容.body是关键问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UNUserNotificationCenter Swift - 在特定情况下不会触发本地通知 的相关文章

  • 如何防止用户控件表单在 C# 中处理键盘输入(箭头键)

    我的用户控件包含其他可以选择的控件 我想实现使用箭头键导航子控件的方法 问题是家长控制拦截箭头键并使用它来滚动其视图什么是我想避免的事情 我想自己解决控制内容的导航问题 我如何控制由箭头键引起的标准行为 提前致谢 MTH 这通常是通过重写
  • CFdump cfcomponent cfscript

    可以在 cfcomponent 中使用 cfdump 吗 可以在 cfscript 中使用 cfdump 吗 我知道 anser 不是 那么如何发出 insde cfcomponent 函数的值 cf脚本 我用的是CF8 可以在 cfcom
  • TIFF 元数据的最大大小是多少?

    TIFF 文件元数据的单个字段中可以合并的元数据数量是否有最大限制 我想在 ImageDescription 字段中存储大文本 最多几 MB 没有具体的最大限制ImageDescription但是 整个 TIFF 文件存在最大文件大小 该最
  • 如何在执行新操作时取消先前操作的执行?

    我有一个动作创建器 它会进行昂贵的计算 并在每次用户输入内容时调度一个动作 基本上是实时更新 但是 如果用户输入多个内容 我不希望之前昂贵的计算完全运行 理想情况下 我希望能够取消执行先前的计算并只执行当前的计算 没有内置功能可以取消Pro
  • 使用.NET技术录制屏幕视频[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 有没有一种方法可以使用 NET 技术来录制屏幕 无论是桌面还是窗口 我的目标是免费的 我喜欢小型 低
  • 如何从日期中查找该月的最后一天?

    如何在 PHP 中获取该月的最后一天 Given a date 2009 11 23 我要2009 11 30 并给出 a date 2009 12 23 我要2009年12月31日 t返回给定日期所在月份的天数 请参阅的文档date ht
  • PHPUnit 和 Zend Framework assertRedirectTo() 问题

    我在创建的测试中遇到了 assertRedirectTo 问题 下面是我使用的代码 public function testLoggedInIndexAction this gt dispatch this gt assertControl
  • 如何使用asm.js进行测试和开发?

    最近我读到asm js规范 看起来很酷 但是是否有任何环境 工具来开发和测试这个工具 这还只是处于规范阶段吗 您可以尝试使用 emscripten 和 ASM JS 1 并从侧分支在 firefox 构建中运行它 有关 asm js 的链接
  • 从超立方体图像中获取文本的确切位置

    使用 tesseract 中的 GetHOCRText 0 方法 我能够检索 html 中的文本 并在 webview 中呈现 html 时 我能够获取文本 但图像中文本的位置与输出不同 任何想法都非常有帮助 tesseract gt Se
  • 节拍匹配算法

    我最近开始尝试创建一个移动应用程序 iOS Android 它将自动击败比赛 http en wikipedia org wiki Beatmatching http en wikipedia org wiki Beatmatching 两
  • Spring Boot @ConfigurationProperties 不从环境中检索属性

    我正在使用 Spring Boot 1 2 1 并尝试创建一个 ConfigurationProperties带有验证的bean 如下所示 package com sampleapp import java net URL import j
  • 循环内的异步性

    我正在使用 jQuery getJSON 用于从一组实用程序的给定 URL 检索数据的 API 我真的很想找到一种为每个实用程序重用代码 完全相同 的方法 由于循环的执行与 ajax 调用无关 因此我无法找到保留循环值的方法 我知道这个描述
  • neo4j - python 驱动程序,服务不可用

    我对 neo4j 非常陌生 我正在尝试建立从 python3 6 到 neo4j 的连接 我已经安装了驱动程序 并且刚刚开始执行第一步 导入请求 导入操作系统 导入时间 导入urllib 从 neo4j v1 导入 GraphDatabas
  • 使用 xpath 和 vtd-xml 以字符串形式获取元素的子节点和文本

    这是我的 XML 的一部分
  • 如何使用 Pycharm 安装 tkinter? [复制]

    这个问题在这里已经有答案了 I used sudo apt get install python3 6 tk而且效果很好 如果我在终端中打开 python Tkinter 就可以工作 但我无法将其安装在我的 Pycharm 项目上 pip
  • 如何将输入读取为数字?

    这个问题的答案是社区努力 help privileges edit community wiki 编辑现有答案以改进这篇文章 目前不接受新的答案或互动 Why are x and y下面的代码中使用字符串而不是整数 注意 在Python 2
  • NotImplementedError:无法将符号张量 (lstm_2/strided_slice:0) 转换为 numpy 数组。时间

    张量流版本 2 3 1 numpy 版本 1 20 在代码下面 define model model Sequential model add LSTM 50 activation relu input shape n steps n fe
  • Erlang dict的时间复杂度

    我想知道 Erlang OTP 是否dict模块是作为哈希表实现的 在这种情况下它是否能提供这样的性能 平均情况 Search O 1 n k Insert O 1 Delete O 1 n k 最坏的情况下 Search O n Inse
  • 在 Nexus 7 2013 上更改方向时 CSS 媒体查询不起作用

    我目前正在我的笔记本电脑 台式电脑和 Nexus 7 2013 上测试 CSS 媒体查询 除了 Nexus 7 之外 它们在台式机和笔记本电脑上都运行良好 当我更改方向时 除非刷新页面 否则样式不会应用 例如 以纵向模式握住设备时 页面正常
  • 如何在react-highcharts中使用图表工具提示格式化程序?

    如何使用图表工具提示格式化程序 我正在使用高图表的反应包装器 我有这样的配置 const CHART CONFIG tooltip formatter tooltip gt var s b this x b each this points

随机推荐