If a user taps on notification that got fired few days before how to identify the date of firing? I wanted to save the date and status into the app.
Fire date property is giving only the start date for the notification. Is there any way to achieve this?
No, for a repeating local notification you can't know exactly which notification was used to open the app because they're all exact duplicates of each other.
Each time the app is opened you could remove the local notification and create a new one. This would only bound the issue, and you could do the same with a date in user defaults.
You could create explicit notifications instead of repeating if there are enough slots, this is the only way to get close to your requirement with local notifications,
The alternative is push notifications where you can get the server to add additional date info to the push.
Use the property userInfo to store the repeating interval (e.g infinite or normal). In the userInfo dictionary , store the key "IsInfinite" and value as "Yes/No". By this way you can differentiate the notification type.
FYI: check the local notification object information also. Is repeat property is available or not?
Since my app works locally so I cannot go for push notifications. Now I am firing notification in a for loop for next 90 days as a possible solution. Its seems like defect with iOS local notifications.
Related
Is there any way to know if a local notification has been delivered so that I can change UI based on it.
For example, time and date is shown when the task is scheduled. If the notification is delivered; i want it to say ‘Time’s up’
I'm building a reminders app that sends local notifications that repeat based on a user's custom settings. For example, I may send a notification every other day at 8pm. Every time I send one of these notifications, I'd like to replace any previous notification that might be displayed in the user's notification center already.
The new UserNotifications framework in iOS gets me close to being able to do this. As far as I can tell, notifications can be replaced in two ways:
Use a UNCalendarNotificationTrigger with repeats: true. Repeated triggers replace past ones.
Create a new UNNotificationRequest with the same identifier. Whenever a request is sent with the same identifier, it replaces all other instances.
Neither of these solutions quite work for me:
I can't use use repeating notifications because UNCalendarNotificationTrigger uses DateComponents for a repeating schedule which just isn't granular enough. My example above of "every other day at 8pm" can't be described with just DateComponents.
Because I need to schedule several notifications all at once, I must specify unique identifiers, and as such, they don't replace each other when displayed.
I was hoping service extensions would save me here. If I could execute code when I receive a notification, I could programmatically remove old notifications when new ones are triggered. Unfortunately, it appears as if service extensions only work for remote notifications. Even if I could use remote notifications (which is a whole can of worms by itself), this solution kind of bends the rules on what is allowed for service extensions.
Does anyone have any ideas on how to solve this, or am I destined to spam my users' notification centers?
I know that this topic is a duplicate, but I need your help. It is very important for me and the other posts have no really an solution for me.
I have an app (Swift 2) where the user can save entries in core data. These entires will show in a table view.
The user has to set an reminder date (with a date picker) for each entry. This date will use for the fire date of the local notification.
Each entry get 2 local notifications
First fire date: 1 week before the chosen date, second fire date: chosen date
The problem is the Apple limitation of 64 local notifications.
The user can only save 32 entries (32 entries * 2 notifications = 64 notifications)
How can I solve this problem with the limitation?
I know that I can set "reminder" into the Apple calendar instead of local notifications. But this doesn't looks good - this shouldn't be the solution.
I know that I can check on each app start or in the method did receive notification, which notifications should set next. but for this I have to trust, that the user start the app or tap on the received notification. If he or she doesn't do this for a few days => no new notification will set and he or she doesn't get the next notification. This solution isn't very safe.
You could routinely send a push notification to devices that have not contacted the backend in some time and have the remote notification handler setup local notifications up to the limit, see application(_:didReceiveRemoteNotification:fetchCompletionHandler:).
Or, you could setup a background fetch that reschedules local notifications up to the limit using, see application(_:performFetchWithCompletionHandler:). Note that you cannot fully control how often this refresh is performed, but in my experience it is called up to a number of times a day.
When using either one of the above method, you can update local notifications even if the user never opens the apps.
I also had some kind of problem like this.
So what I did is that I planned the notifications as much as possible, and when there is room to plan more notifications then I planned more notification.
UIApplication.sharedApplication().scheduledLocalNotifications?.count
You can get the number of planned notifications. Subtract it from 64 and plan remaining notifications.
In applicationWillEnterForeground you can do this -
if UIApplication.sharedApplication().scheduledLocalNotifications?.count < 64
{
//refersh list
let nc = NSNotificationCenter.defaultCenter()
nc.postNotificationName(Constants.SCHEDULE_MORE_NOTIFICATION, object: nil)
}
So your notification list will be refreshed automatically.
I would like to know what is the best approach to do if I want to fire notifications for more than one time each day everyday.
I did some research and read that notifications for the next day cannot be fired unless the user opens the app the next day and updated the notification. Is that true? is there anyway I can do it without the need of the user opening the app everyday?
Thank you
You can schedule up to 64 local notifications. There is no limit on the time period; you can schedule them years in advance if you like.
That said, if you need some mechanism to schedule new notifications, even if the app is not running at all (e.g. because the user terminated it), you need a background mode for that. Fetch is probably the way to go here, as it doesn't need a special trigger. You could also send silent push notifications in order to wake the app, make the calculations and schedule the new notifications.
The previous posting on here regarding deleting notifications from the notification center claim its not possible to delete individual notifications, only all of them.
However individual notifications do get deleted for the reminder app - set 3 reminders to fire in a couple of minutes, when they fire go to the notification center, now select one, after the reminder app launches go back to the notification center and that specific notification has been deleted but others remain. So how is this achieved?
The Reminders app probably fires local notifications. Local notifications can be withdrawn, using cancelLocalNotification: on UIApplication.
(Additionally, push notifications when sent using the enhanced call (first byte is 1) supports an expiry parameter (when sending, not inside the JSON payload) that is supposed to mean that this notification, if not delivered by a certain date, should not be delivered. It is possible that this parameter is also used in a similar way to hide received notifications.
It is also highly possible that Apple's own apps do whatever the hell they want.)
When the user taps on the notification:
If the app was running in the background, you retrieve it using AppDelegate's method didReceiveLocalNotification.
If it wasn't running, then the notification can be obtained with the didFinishLaunchingWithOptions method. You just need to search the launchOptions dictionary for the UIApplicationLaunchOptionsLocalNotificationKey.
If you want to delete specific notifications which have already fired, specially when the user doesn't enter the app by tapping on the notification, then it's probably better to store them in NSUserDefaults so that you can still obtain them later. That approach is explained here.