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.
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.
My app receiving push notification, and showing appropriate info message for that. However when I'm clicking to the message, application becomes active but application didFinishLaunchingWithOptions is not getting called which is right i think, since the application is not suspended and it just resigns active. The question is how i can make sure that user clicked to message when application becomes to foreground ?
I think what you are looking for is this app delegate method:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
It will be called if your app is backgrounded, and the notification payload will be delivered in the userInfo dictionary. This contrasts with the situation when the app is launched from cold start, when this method does not get called, and instead you check in the launchOptions dictionary for the payload.
However the preferred way to do this since iOS7 is to use this:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
This method is called when a user taps on a notification, regardless of whether the app is launched from cold start or foregrounded from background. So even if you are not using the completionHandler, it provides a more consistent way of accessing the notification payload. If this method is present, the older one does not get called.
If I understand the question correctly, you are asking how to be sure that the app was brought into the foreground as the result of the user “clicking” i.e. acting on a push notification.
When the app is not running at all, you can use -application:didFinishLaunchingWithOptions: as you mention. The launchOptions dictionary contains the payload, etc. — I won’t describe this since you already know how this works.
When the app IS running however, that method is not going to be called. Instead, - application:didReceiveRemoteNotification: is called. BTW this is called if the app was already in the foreground OR if it was in the background and the user “clicked” on the push notification banner/alert to open the app. It will not be called otherwise: so I believe this is exactly what you’re looking for.
The userInfo dictionary provided by this method will contain the notifications data, similarly to -application:didFinishLaunchingWithOptions: for more ad-hoc processing. (Note: userInfo and launchOptions are semantically different, but hopefully this is obvious. :))
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
{
}
I am using badge notifications in my app and it works fine but I get the badge number by calling a method. So if method is called then the badge number gets increased but how to call that method while app is closed.
- (void)repeatedMethod {
SOWObject *object =[[SOWObject alloc]init];
[object getBadgeNumber:[self getDBPath]];
// I get badgeArray from above method
[UIApplication sharedApplication].applicationIconBadgeNumber=badgeArray.count;
}
is there any way we can call this method each day when date is changed and update badge number.
So far as I know, you can do this with 3 options:
Use Silent notification - for iOS7 and above only. a bit complicated since you need to enable Push and do back-end intergration
Use Background refresh - Create a timer + UIBackgroundTaskIdentifier. (not 100% sure)
Use Local/Push Notification - disadvantage using this, user knows of such notification is triggered.
implement this delegate method in your appdelegate:
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
This method is fired whenever the OS finds any local notifications.(it doesn't matter whether your app is in the background or in the foreground).
For more information & code look here
Look at the accepted answer.
Edited:
Answer for you to call a method while app is in background is here
So, it basically says not all the apps have access to background execution. Officially it's mentioned in the apple's developer site also: HERE
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).