Random Repeating Local Notifications in Swift
J

0

7

TL;DR: Is it possible to have repeating, random local notifications without using APNS (or an alternative push notification service)?

I am building a Core Data app that includes a bunch of objects. (We'll call them widgets.) Every day at a certain time – let's say noon – I want to send a notification to check out one of these widgets.

In didFinishLaunchingWithOptions I check to make sure Notifications are enabled and set the delegate, etc. and call a function to build the local notification. All this is working perfectly.

    var date = DateComponents()
    date.hour = 12
    date.minute = 00
    let content = UNMutableNotificationContent()
    content.title = "You should see this widget!"
    content.userInfo = ["widgetID": widget.id]
    content.body = "Today's Widget:\n\(widget.title!)"
    content.sound = UNNotificationSound.default()
    let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
    let request = UNNotificationRequest(identifier: notificationIdentifer, content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error : Error?) in if let theError = error { print(theError.localizedDescription)}}

And I have the delegate's handler working correctly, too:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) { ... a bunch of stuff to happen ... }

As it is now, in that function I'm able to "reset" the UNMutableNotificationContent to a new random widget IF the user interacts with the notification in any explicit way. The problem is that if the user doesn't interact with the notification at all — see the "Handling the Standard System Actions" section of Apple's Scheduling and Handling Local Notifications documentation — I have no way to "reset" the notification, so tomorrow they're simply going to get suggested the same widget again, and that's annoying.

I'm not really interested in using APNS (or an alternative) because I don't feel like I should need to do that and don't want to deal with all the extra stuff required, but I cannot seem to find an alternative.

Please note that this question is not a duplicate of:

Jubilant answered 28/12, 2017 at 6:7 Comment(8)
Instead of scheduling the local notification to repeat itself, you might schedule it only once the first time and then every time the user interacts with the popup.Ethelinda
@AndreaMugnaini Right. As I explained, though, that's what I'm doing now. The problem is if the user ignores or swipes to dismiss the alert, they are simply going to receive the same alert the next day.Jubilant
You say: "they are simply going to receive the same alert the next day". If you avoid the repeat it (repeats: false), why should it be there the next day?Ethelinda
@AndreaMugnaini I want the notification to repeat every day; But I also want the content included in the widget to be different every day.Jubilant
@DavidVincentGagne did you ever get this system working or have you ended up going another route? ThanksAerator
Hey @Simon: No, I never really got an answer here. But I realized that if the user does not interact with the notification at all, the fact that it's the same the next day doesn't matter in my use case.Jubilant
@DavidVincentGagne thanks for the answer after all this time since your comment, I actually made my piece with that also in the meantime. Thanks again!Aerator
has anyone worked out how to create dynamic content for location notifications yet?Muriate

© 2022 - 2024 — McMap. All rights reserved.