Repeat notification with different time - ios

I have created a local notification on 10:00 AM and I want to repeat the notification based on a calculation for the next time when the first notification triggered.
So how can I listen to the notification trigger to create the next notification?
Note: I have used didRecieve notification delegate but it works only if the app in the foreground I want the same thing for the background.

time interval trigger would do it.
https://developer.apple.com/documentation/usernotifications/untimeintervalnotificationtrigger
Use a UNTimeIntervalNotificationTrigger object for local notifications that you want delivered at a time relative to the current time. You specify the number of seconds that must elapse before the notification fires. You can also set up the trigger to repeat at the specified interval.

Related

Display Local Scheduled Repeating Notifications on Condition

I am using flutter_local_notifications plugin to schedule notifications, but I want to achieve two things.
The scheduled notification should run some code every time, before showing the notification to check whether or not to display the notification.
I want to specify a custom time interval, instead of the options given by RepeatInterval

Can a Local Notification triggering be detected without tapping on the notification?

My app starts a timer when the local notification based on date is triggered using UNUserNotificationCenterDelegate::didReceive delegate.
But what if the app is already opened when notification is triggered, I want my timer to start automatically at that point, without tapping on the notification.
Is this possible ?
e.g:
I want to set a notification for 3 pm, at that point start a 30 min timer (this should start at exactly 3 pm), that will expire at 3:30 pm.
What if the user taps on notification 10 mins later, at 3:10 pm...the timer will expire at 3:40.
That is why I want to start the timer when notification is triggered, not when the user taps on it.
Method userNotificationCenter(_:willPresent:withCompletionHandler:) will be called when a notification is delivered to a foreground app.
Source : Link
Use this method for using your timer concept
application:didReceiveLocalNotification:
But yes when your app is in background state, and if local notification come, you won't receive any method call.

skip one notification, repeating each day

I'm making an app, reminding user to do smth each day, and I can't implement the situation , when notifications are shown or not shown depending on some condition even if the app is in background mode.
one possible variant is to set endless repeating notifications:
notification.repeatInterval = NSCalendarUnit.NSDayCalendarUnit
but then some method is required to skip notification for present day if some condition was satisfied. If one removes these notifications from Notification Center they won't fire next day.
The other way is to make some method creating notification only for present day, and then removing them if some condition is satisfied. The problem is, this method should create these scheduled notification even being in background mode, and I have no idea how to make that.
Are there any ideas?
Unfortunately the only way you can wake up your app at a specific time is through a silent APNS push. This means you need a server that will send silent pushes to your device, the user needs to have internet and the user needs to opt out for push.
Maybe background fetch will work for you ? With Background Fetch your app gets woken up every minimum interval. You could set your app to wake up every x minutes and check that condition.
[application setMinimumBackgroundFetchInterval:100]; //seconds
Please beware that:
The minimum number of seconds that must elapse before another
background fetch can be initiated. This value is advisory only and
does not indicate the exact amount of time expected between fetch
operations.

What is the best way to modify a UILocalNotification after it has fired?

I understand that you cannot modify a UILocalNotification after it has been scheduled. My problem is that I want to set the .repeatInterval so that it fires at a specific time once per day. However I want the content to change every day. So the user sets the alarm to fire at 10AM, and every day at 10AM gets an alert with different content without having to reset their alarm.
I was thinking about canceling and rescheduling when didReceiveLocalNotification was called but that requires the user to launch the app before the next scheduled alert. Is there a way to call a method immediately after the alert is fired?
Unfortunately you have no certain way of knowing that a local notification has fired unless your app is frontmost at the time it fires.
And, as you say, you cannot change a notification.
So I think your best bet is do not use repeating notifications. Set up a different single notification for each day, say, two weeks ahead, with different messages for each, and hope the user runs the app between now and then so that you can "refresh" the queue, as it were. Maybe make only the last one repeating, just in case, so that the notifications never stop - but of course then the message will be same each time. But there's nothing you can do about that. You can't force the user to run the app.

How to delete local notification if current datetime is past the scheduled datetime?

I am using local notification for my app.
Notification is set for particular time interval like daily, weekly, monthly and it also have completion time date.
So how can i delete notification when it's completion time date pass away?
For example
If I set tomorrow as completion time date and notification repeat interval daily then this notification will show only today and tomorrow, not after that.
Thanks in advance
You can just schedule individual notifications in this case. For example if you want to have a notification today and tomorrow, that's two notifications. A "completion time" is not available in the UILocalNotification API. Please note that you can have at most 64 scheduled notifications at the same time. So the user has to open the app at least every 64 notifications, so that your app is able to schedule new notifications. (If you have more than 64 local notifications, all but the soonest firing 64 notifications are discarded.)

Resources