I am developing an app where I need to alert the user using UILocalNotification for a specific time interval
The problem is that I place the code in applicationDidEnterBackground..
So evrytime the user close the app. a New notification is created, resulting in multiple notifications created. How do I go about this problem? Thank you
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
Related
I am developing an app which only works in 8.30am to 5.30pm. I want to store the data only in between 8.30am to 5.30 pm. I used local notification for doing so. But it only works when user tap the notification.In 8.30am and 5.30pm, i need to execute some code even if the app is killed. Is there any other mechanism to do so...?
Here is my code:
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification!=nil)
{
[self application:application didReceiveLocalNotification:notification];
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
code to be executed;
}
There is no way to execute a method in your app if the app is killed. You can bring the app up in the background using silent notifications. But silent notifications are better suited for News apps or apps which need to download content in the background so it is readily available for users when the app comes to the foreground.
Apart from this, the only way to execute the method is when it is either in the foreground or at least active in the background (using one of the available background modes). If your app is using a background mode only to stay in the background, Apple will reject the app, so be careful.
In birthday app which is the best place to check birthdates and set its UILocalNotification in iOS.
Iss didfinishlaunching the best place? or any other.
That is a good place to schedule the notification if you need to check it only once.
If data changes often I suggest you put it in applicationDidBecomeActive: since iOS might not remove the app from memory each time.
You also need to make sure that you schedule only one notification, and not each time the application is started.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(#"Notification fired"!);
}
if your in foregroud it will call this method. then you can use this method also.
I'm using a Beacon device for testing. The scenario is as follows.
I'm throwing a local notifications when receiver enters the location which is based on latitude & longitude range which i have set manually. This works fine and i'm clearing this notification on didFinishLaunchingWithOptions. When I enter to the next closest region i'm getting the notification and on clicking it I can able to present a view. But when i come out of the app the local notifications are not getting cleared. What may be the problem?
I'm using two different methods for two different regions.
1st one is
-(void) didEnterLocation:(SLMLocation*)location
{
// throwing a local notification
}
the above method's notification is getting cleared in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
the second method is as follows: this is for 2nd region
- (void) didEnterNewClosestRegion :(SLMBLERegion*) region
{
// throwing local notifications here & notifications are visible
}
Now in the second method only I can't able to clear the local notification. Your help is highly appreciated.
try this may be help full ..
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
Thanks
This is because didFinishLaunchingWithOptions method is only called one in app life time, there is a possibility that it is already running in background so the notifications will not be cleared. I would suggest you to move cancelling code to applicationDidBecomeActive.
cancelAllLocalNotifications, cancels the delivery of all scheduled local notifications, it doesn't clear already presented notifications from lock-screen or notification-center.
https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/cancelAllLocalNotifications
When the user leaves my iOS app, I want to schedule a local notification to remind him about my app. What is a correct place to do it? I cannot choose between
- (void)applicationWillResignActive:(UIApplication *)application
and
- (void)applicationDidEnterBackground:(UIApplication *)application
Or there's no difference in the case?
I also have a second question. When my app launches (either if user pressed notification, or from launchpad), I should obviously remove all that reminding notifications. I guess I should do it in
- (void)applicationDidBecomeActive:(UIApplication *)application
or
- (void)applicationWillEnterForeground:(UIApplication *)application
or maybe another method? Or it makes no difference again?
Do it all in applicationWillEnterForeground. Remove any old notifications that don't matter now the user it engaged with the app and install any new notifications for after this use session. If the notification fires when the user is still using the app then you don't need to display anything (and the system won't display anything either).
I have the following issue: "My app receive some remote notifications from an own server just to show to the user some practical information. I am not using a icon badge, because I don't need it. If the application user touch the remote notification from the iOS Notification Center my application can catch it without any problem, I receive the options from application:didFinishLaunchingWithOptions: or, if the application is open I catch the remote notification with application:didReceiveRemoteNotification: selector. But, now I want to remove these notifications from iOS Notification Center because It is just a message and I have been looking for the answer in another posts and I've tried these solutions in my app and they don't work"
Some solutions were the next:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[application cancelAllLocalNotifications];
application.applicationIconBadgeNumber = 0;
...
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
if (launchOptions) {
[application cancelAllLocalNotifications];
application.applicationIconBadgeNumber = 0;
}
...
}
And the remote notification still in the iOS Notification Center, how can I remove from that place without any tricky code or is an iOS SDK issue? I don't think that an issue was possible because Tweetbot app remove its remote notifications from iOS Notification Center after you enter to the app.
Regards!
With introduction of UNUserNotificationCenter for iOS 10 and above, it is now possible to remove few or all remote notifications of your app.
UNUserNotificationCenter documentation
With a shared singleton instance of this class, it is possible to manage delivered remote notifications on the device. Specifically, the following methods can be used:
func removeDeliveredNotifications(withIdentifiers: [String]) if you want to remove specific notification of your app, OR func removeAllDeliveredNotifications() to remove all notifications of your app.
First of all make sure you haven't set badge notification to be off in the control panel (I noticed that if the badge has a number to begin with, then badging is turned off in the control panel, it cannot be set to 0).
If its not turned off then in addition to setting applicationIconBadgeNumber to 0, also try canceling all local notifications (even if you haven't queued any, if you have first get a list of them, then cancel them, then register them back again). Yes clearing local notifications can have an effect on being able to clear the badge number for remote notifications.