5

I've been working on setting up local notifications for my app in iOS 10, but when testing in the simulator I found that the notifications would be successfully scheduled, but would never actually appear when the time they were scheduled for came. Here's the code I've been using:

let UNcenter = UNUserNotificationCenter.current()
        UNcenter.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
            // Enable or disable features based on authorization

            if granted == true {

                self.testNotification()


            }

        }

Then it runs this code (assume the date/time is in the future):

func testNotification () {
    let date = NSDateComponents()


    date.hour = 16
    date.minute = 06
    date.second = 00
    date.day = 26
    date.month = 1
    date.year = 2017

    let trigger = UNCalendarNotificationTrigger(dateMatching: date as DateComponents, repeats: false)

    let content = UNMutableNotificationContent()
    content.title = "TestTitle"
    content.body = "TestBody"
    content.subtitle = "TestSubtitle"

    let request = UNNotificationRequest(identifier: "TestID", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request) {(error) in
        if let error = error {
            print("error: \(error)")
        } else {
            print("Scheduled Notification")
        }

    }
}

From this code, it will always print "Scheduled Notification", but when the notification is supposed to be triggered, it never triggers. I've been unable to find any fix for this.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Simply Epic
  • 51
  • 2
  • 4
  • is your application in the foreground when the notification is supposed to be triggered? – bhakti123 Jan 27 '17 at 11:13
  • No, I make sure to exit the app before the notification triggers. I've tried it when the app is in the background, completely closed out of, and when the lock screen is showing. None work. – Simply Epic Jan 27 '17 at 15:03
  • are you manually changing the time of the device to test it out? – bhakti123 Jan 27 '17 at 16:16
  • @bhakti123 Before I test, I check the time and change the date.hour and date.minute to be a few minutes ahead of the time. I then test, and it gets the notification scheduled, and I close out of the app. I don't change the device's time at all, just the time to schedule the notification. – Simply Epic Jan 27 '17 at 19:29
  • 1
    try changing the identifier "testId" to something else, notification with same identifier are rejected. So maybe it had scheduled a notification the first time, and that's why it is rejecting the other ones. – bhakti123 Jan 28 '17 at 09:17
  • @bhakti123 Just tried switching the Identifier. Didn't work. – Simply Epic Jan 29 '17 at 02:00
  • @bhakti123 I just moved the above notification code into a new project file, and it's working properly, so there must be something else wrong with my project outside of this code. Not sure what might be affecting the notification that isn't in this code, though. – Simply Epic Jan 29 '17 at 02:35
  • Ok. Good to hear. – bhakti123 Jan 29 '17 at 06:31
  • Still no idea what was wrong, but I created a new project and moved all my files over from the old one to the new one, and it's fixed. – Simply Epic Feb 02 '17 at 23:26
  • Did you change app's bundle id in the new project? – Makalele Sep 17 '21 at 13:33

1 Answers1

2

Here are a few steps.

  1. Make sure you have the permission. If not, use UNUserNotificationCenter.current().requestAuthorization to get that. Or follow the answer below if you want to show the request pop up more than once.

  2. If you want to show the notification foreground, having to assign UNUserNotificationCenterDelegate to somewhere.

This answer might help.

Allen
  • 2,979
  • 1
  • 29
  • 34
  • assigning NUserNotificationCenter.current().delegate will "break" background push notifications (UIApplicationDelegate.didReceiveRemoteNotification() is no longer called) .. AFAIK there is no proper way to implement foreground notifications ATM – Erkki Nokso-Koivisto Sep 15 '21 at 11:01