iosswiftunusernotificationcenterusernotificationsnsusernotification

can UNUserNotificationCenter add multiple requests?


I can add one request. But when i tried to ad two, only the latest is fired. The first just doesn't appear. Here is my method :

            var triggerDailyEnd = DateComponents()
            triggerDailyEnd.hour = hour
            triggerDailyEnd.minute = minutes
            triggerDailyEnd.weekday = i+1
            // this repeat every day
            let triggerEnd = UNCalendarNotificationTrigger(dateMatching: triggerDailyEnd, repeats: true)

            let identifierEnd = getNotificationId(i+1, hour, minutes,true)
            let requestEnd = UNNotificationRequest(identifier: identifierEnd, content: notifEnd, trigger: triggerEnd)

            notificationCenter.add(requestEnd)

            var triggerDailyStart = DateComponents()
            triggerDailyStart.hour = hour
            // correctly remove time
            triggerDailyStart.minute = minutes-Int(ProgrammationViewController.PREHEAT_TIME)
            triggerDailyStart.weekday = i+1
            // this repeat every day
            let triggerStart = UNCalendarNotificationTrigger(dateMatching: triggerDailyStart, repeats: true)

            let identifierStart = getNotificationId(i+1, hour, minutes,false)
            let requestStart = UNNotificationRequest(identifier: identifierStart, content: notifStart, trigger: triggerStart)

            notificationCenter.add(requestStart)

the code is quite simple.When I add requestEnd after requestStart, the notification of requestEnd is fired and not requestStart.And vice versa. Strange ?


Solution

  • The identifier parameter to the request needs to be unique. If it's the same, the second request will overwrite the first.