簡體   English   中英

UNUserNotificationCenter Swift - 在特定情況下不觸發本地通知

[英]UNUserNotificationCenter Swift - Local Notification Not Firing in specific cases


我在應用程序中使用本地通知時遇到問題。

我正在安排包含自定義內容和自定義操作類別的本地通知。 它在 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
>>

正如我在 2 小時前設置的那樣,它在中午 12 點正確設置。
它在 iOS 11 台設備上運行良好。 但不能在 iOS 10 工作。
更新的代碼有什么問題嗎? 或者其他任何需要處理的。

請讓我知道解決方案。
提前致謝。

要生成本地 Notification, Content.body不能為 null 或為空。

萬一在解析blank到 body 時,iOS 10 不會生成本地推送。 但它在 iOS 11 工作。

Content.body的值是關鍵問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM