how to implement multiple local notification not a single notification for swift 3

后端 未结 4 1320
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-15 17:03

i have use the single notification , and this is my code: this is for register the local notification>>>

    func registerLocal() {
    let center = UNUserNotifi         


        
相关标签:
4条回答
  • 2021-02-15 17:44

    Note that your code should works as expected! however, there is a little pitfall.

    I think that I almost faced the exact same issue, after I tried to trace the pending notifications, I noted that the only notification that has been added is the last requested one. That's because you are calling in scheduleLocal() function:

    // not required, but useful for testing!
    center.removeAllPendingNotificationRequests()
    

    Well, it is good to remove any existing notification reminder requests, this would help preventing unnecessary duplicate notifications, but you should call it only once before calling scheduleLocal():

    UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    // now you could call this function many times...
    scheduleLocal()
    

    simply, you don't want to delete each pending notification directly after adding it, instead you delete the whole previous pending notifications and add new ones.

    0 讨论(0)
  • 2021-02-15 17:46

    Call this function with different parameters. Like i called when app goes background

      func applicationDidEnterBackground(_ application: UIApplication) {
    
            setNotification(time: 25,identifier: "notific2")
            setNotification(time: 50,identifier: "notific3")
            setNotification(time: 90,identifier: "notific4")
        }
    
    
      func setNotification (time : Int,identifier : String)
        {
            let content = UNMutableNotificationContent()
            content.title = "Don't forget"
            content.body = "Buy some milk"
            content.sound = UNNotificationSound.default()
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(time),
                                                            repeats: false)
            let center = UNUserNotificationCenter.current()
            // Swift
            let request = UNNotificationRequest(identifier: identifier,
                                                content: content, trigger: trigger)
            center.add(request, withCompletionHandler: { (error) in
                if error != nil {
                    // Something went wrong
                }
            })
        }
    
    0 讨论(0)
  • 2021-02-15 17:53

    You can call the func scheduleLocal() multiple times with different dateComponents to schedule on different dates. Or you can pass a array of dates to this function and run a loop to schedule the notifications according to these dates.

    Just make sure that the identifier you pass in UNNotificationRequest(identifier:, content:, trigger:) function is different for each notification.

    Hope this helps. :)

    0 讨论(0)
  • 2021-02-15 18:06

    Use a different request identifier for each notification (otherwise you only see the last notification). In the above example, ensure request identifier "UUID().uuidString" contains a unique value for each notification request.

    0 讨论(0)
提交回复
热议问题