UNUserNotificationCenter Swift-在特定情况下不触发本地通知 [英] UNUserNotificationCenter Swift - Local Notification Not Firing in specific cases

查看:190
本文介绍了UNUserNotificationCenter Swift-在特定情况下不触发本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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


I'm facing issue while using Local Notifications in application.

我正在安排具有自定义内容的本地通知&自定义操作类别.它是调度和在iOS 11中触发,但在iOS 10中不触发.

I'm scheduling local notification with custom content & custom action category. It's scheduling & firing in iOS 11, but not in iOS 10.

根据文档,它也应在iOS 10中运行.

As per documentation, it should be worked in iOS 10 as well.

请查看我的代码,并告诉我为什么在这种特定情况下它不起作用.

Please review my code, and let me why it's not working in such specific cases.

代码在下面.

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

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

This code successfully works & schedules local notification. And resulting as below:

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

我在2个小时前设置的时间已正确设置为中午12点.
在iOS 11设备上运行正常.但无法在iOS 10中使用.
更新的代码有什么问题吗?还是需要处理其他任何事情.

It's setting properly on 12 PM as I set before 2 hours.
It's working fine in iOS 11 devices. But not working in iOS 10.
Anything wrong with updated Code? Or anything else need to be handle.

请让我知道解决方法.
预先感谢.

Please let me know about solution.
Thanks in advance.

推荐答案

生成本地通知.
Content.body 不能为空或空白

To generate local Notification.
Content.body must not be null or blank

在将空白解析为正文的情况下,iOS 10不会生成本地推送.但是它可以在iOS 11中使用.

In case while parsing blank to body, iOS 10 will not generate local push. But it's working in iOS 11.

Content.body 的值是关键问题.

谢谢.

这篇关于UNUserNotificationCenter Swift-在特定情况下不触发本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆