且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何显示多个本地通知?

更新时间:2023-01-15 07:45:10

使用循环注册具有唯一标识符

Using for loop to register multiple Notification with the unique identifier https://developer.apple.com/documentation/usernotifications/unnotificationrequest/1649634-identifier?language=objc

let notificationRequest = UNNotificationRequest(identifier: "cocoacasts_local_notification", content: notificationContent, trigger: notificationTrigger)

您应该更改此标识符"cocoacasts_local_notification"以动态重置唯一标识符

you should change this identifier "cocoacasts_local_notification" to dynamically reset the unique identifier

 let notification = UNMutableNotificationContent()
        let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dayComponent, repeats: true)
                        let lnMessageId:String = messageDict["Id"] as! String
                        let dayRequest = UNNotificationRequest(identifier: lnMessageId , content: notification, trigger: notificationTrigger)
                        UNUserNotificationCenter.current().add(dayRequest, withCompletionHandler: {(_ error: Error?) -> Void in
                            if error == nil
                            {
                                //print("success")
                            }
                            else
                            {
                                //print("UNUserNotificationCenter Error : \(String(describing: error?.localizedDescription))")
                            }
                        })