I'm trying to schedule repeating local notifications and set the application badge number to the actual number of notifications that have been scheduled so far at any given moment.
Since there's apparently no way to set a different badge number for each occurrence of the notification, I can see only 3 solutions:
Ignore the repeatInterval property of the notification and schedule separate notifications myself (see here). The thing is, since the system holds only 64 notifications per application, I may miss notifications if the user taps "dismiss" too many times, especially if several notifications are active.
Show an incorrect badge number (1 for each unique notification, regardless of the number of times this notification has been repeated so far).
Not use a badge at all.
Am I missing something, or are these the only 3 options?
Looks like those are your options, since the badge number property of an UILocalNotification does not increment the current applicationIconBadgeNumber but just set it's these are your options.
Also keep in mind that when going with option 1 your have a maximum of 64 notifications that can be set, but you know that already ;)
Related
I created a local notification with Swift 3. The problem is that I want to change the body of local notification.
For example:
- There are 15 left issues.
20 minutes later:
- There are 10 left issues.
Can anybody help me, please?
After research I can almost confirm that it is not possible to update the repeated notification without app being foreground.
When you schedule a repeated push notification, the title and body is already set. In order to change the tile and body we will need to catch notification. However, according to this answer, notification service extension only works for remote notification but not local notification.
SO... How to achieve this using work arounds?
I can think 2 ways to do this.
Schedule approx 60 notification manually and preset the title and body. However, in your case, it is still not possible because you will never know how many issues left after 20 mins?? If you can pre-calculate, then you can use this solution. The disadvantage for this is it can only schedule for 64 local notifications and each time when push a local notification it creates a new delivered notification stay in the system which will spam user's notification center.
The second way is to use Notification Content Extension. NOTE: You can still not modify the title and body for the notification delivered. However, you can create a custom screen when user force touch the notification bar. Maybe the subtitle can be something like force touch to see how many issues left and implement a custom screen to show the number of issues when user force touch on it.
I am still experiment on this. There's one more option that I want to suggest is you can always do a remote push notification if issue number changes, which might be a better user experience in your case instead of pushing a repeated one every 20 mins.
Useful Reference
Here is a tutorial I found useful for Notification Content Extension.
To understand more, here is a link to WWDC of introducing notification service extension and notification content extension.
I believe you can use Notification Content Extension. You kinda have much more freedom since you are working with a view controller. I would use that!
I'm trying to set local notification in swift firing once at a definite date, but it seems there is no such option for repeat interval . By the default it triggers every 24 hours. Is there any way to submit a notification, which fires only once, so that user didn't have to even open the app or etc?
You have to set the repeatInterval of your UILocalNotification to 0:
If you assign a calendar unit such as weekly (NSCalendarUnitWeekOfYear) or yearly (NSCalendarUnitYear), the system reschedules the notification for delivery at the specified interval. Note that intervals of less than one minute are not supported.
The default value is 0, which means that the system fires the notification once and then discards it.
Regarding your second question: no! The user has to open the app at least once! Otherwise the app does not execute any of its code. Register the notification on first launch.
In my app, I want to send a Local Push notification every 30 minutes. One way is to just configure local push notification and fire it. However there is a possibility that user can change his time and move forward 30 minutes. In this way a cheat can be done.
I want to configure my app so that notification only occur after 30 minutes. How can I do that. My app does communicate with server and can get its timestamp but I want to do things which don't use much server resources.
The only way I can think of to detect a user altering the system clock is as follows:
When app launches, ask your server the time and note the difference between that and [NSDate date]. Persist that as [NSNumber numberWithFloat:serverOffset];
Implement a method like - (BOOL)deviceClockChanged that asks the server again and compares to the persisted value. If the difference is greater than some small tolerance for clock drift + latency on the synch request, then you can conclude that the clock was changed. Do all this in UTC so it works independent of user travels between time zones.
Consider this: if the user wants badly enough to fool your app about the time in order to delay a notification, messing up the rest of his phone, maybe you ought to just let him edit the notification schedule.
I can supply code examples for points 1 and 2 if you want, and if you want I can supply some #"alert text" for point 3 that will make the user feel really guilty about editing his notifications.
My original answer here. If you choose to let user edit notifications, these methods will be key... UIApplication has a property:
NSArray *scheduledLocalNotifications;
and implements:
- (void)cancelLocalNotification:(UILocalNotification *)notification
So to change one, cancel it, then reschedule it.
i want to send a notification each day at the same time with different information. I got no problem with showing the alert when i want to. The problem is what to show. First i tried to schedule one alert every day with different information in them, but that seems really inefficient. Then i tried with
-(void)application(UIApplication*)app didReceiveLocalNotification:(UILocalNotification *)Notif{}
but found out that it runs when the application is running in foreground only.
Is there any way to provide an array or something like it with the alert and tell the alert to get its body from the array with different index for different days?
Or should i do it with some notification that can run when the app is in background mode and the alert show and make a new alert?
All information must be provided when you schedule a local notification, so no, a notification's body cannot be sourced from an array as you described. If users are likely to open your app often and you know notification content in advance, perhaps you could ensure that x notifications are always scheduled (could be achieved by inspecting UIApplication's scheduledLocalNotifications).
This solution isn't ideal though, as it relies on users opening your app on a (somewhat) regular basis. I think push notifications are probably your best bet, but that requires access to a server, etc.
Yeah push could prob be the solution. But i dont have a server for that. The users will most likeley not open it any more after they have opened it the first time to activite the local-messages. So then this isnt really possible to do without push. when i tried a for-loop to schedule 250 days with different text each day it took about 2 minutes on iphone 4 so its not duable.
I'm trying to understand the applicationIconBadge property of UILocalNotification. I can only set an explicit badge value, I can't specify I just want it incremented when the notification fires.
http://developer.apple.com/iphone/library/documentation/iPhone/Reference/UILocalNotification_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40009565-CH1-SW11
I'm trying to accomplish the following: one notification fires, the user ignores it, a second notification fires, the user ignores it. At this point I would like the badge to be 2, but as far as I can tell there aren't any methods I can implement to handle the notification firing, but being ignored. My didReceiveLocalNotification event will not be called. That means there is no way for me to manipulate the badge number, aside from the UILocalNotification.applicationIconBadge property, which has no "just increment it" functionality.
One way I can think of doing this is to maintain a count internally, and when you create each notification, assign it the right badge property. So if you know that notification 1 will fire before notification 2, then assign the first one 1, and the second one 2. This will take care of your issues as stated above.
If the user launches the app after notification 1 but before notification 2, and gets rid of the first '1' badge, then you can then reassign the badge properties for all the remaining notifications. It's more work than doing a straight += 1, but unless someone else has a better idea, I think this is the way to do it.
Check out this category I made for UILocalNotification: GitHub UILocalNotification
I have reason to believe that the implementation of applicationIconBadgeNumber is buggy, or at least not according to what it has been described to do in the documentation.
I've filed a bug report/enhancement at http://bugreport.apple.com/
You may take a look at my report archived at Open Radar: http://openradar.appspot.com/radar?id=767401
Meanwhile, I have also resorted to a similar hackery just to achieve this simple behaviour.
According to Marco, developer of Instapaper, Apple prioritizes enhancement and feature requests partly based on how many developers are requesting them.
If this feature is important to you I urge you to file a report as well.