iOS Handle notifications after starting app from not-running state - objective-c

I've been trying to handle receiving notifications in my app, but its not really working out.
When I use didReceiveLocalNotification:(UILocalNotification *)notification. I can receive and use the notification that is used to enter the app, without any problems
However, this function is only fired when the app is already running (active, inactive, background, and possibly suspended, but I haven't tried that yet).
Now, there is this function didFinishLaunchingWithOptions:(NSDictionary *)launchOptions where you can use [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey] which would return a UILocalNotification.
However, when you launch the app from not-running state, this event is not fired. The LocalNotification then opens the app, but I can not use it in any way.
Now, my question is: How can I make it work, so I can receive and process notifications when starting the app, from a notification, when the app is in not-running state? Is there perhaps something I'm doing wrong here?
Here is a bit of sample code from my app:
First, the didFinishLaunchingWithOptions function, which, unfortunatly does not work. The function [sharedLocalNotificationsInstance processNotification:notification] is never launched...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
LocalNotificationsController *sharedLocalNotificationsInstance = [LocalNotificationsController sharedLocalNotificationsInstance];
[sharedLocalNotificationsInstance checkNotifications];
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if ( notification != nil ) {
// Process the received notification
[sharedLocalNotificationsInstance processNotification:notification];
application.applicationIconBadgeNumber = 0;
}
return YES;
}
And a second piece of code: The didReceiveLocalNotification function, which works perfectly: I receive the notification, and [sharedLocalNotificationsInstance processNotification:notification] works perfectly.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
// Used when the application launches from a notification
LocalNotificationsController *sharedLocalNotificationsInstance = [LocalNotificationsController sharedLocalNotificationsInstance];
// Process the received notification
[sharedLocalNotificationsInstance processNotification:notification];
}

This is how iOS handles local notification. It depends on which state of your app, e.g. active, running in background, or not started yet. The iOS will invoke either didFinishLaunchingWithOptions or didReceiveLocalNotification, or won't touch your app at all.
Please see this article for clarification - http://www.thekspace.com/home/component/content/article/62-uilocalnotification-demystified.html

<Matrix-Morpheus-Meme title="WHAT IF I TOLD YOU">
When an application is launched from the "not-running" state because a user tapped on a local notification alert, the application has been started by iOS, not by Xcode, thus IT IS NOT RUNNING UNDER THE DEBUGGER. You cannot breakpoint inside it and neither does NSLog() send anything to the Xcode console. Test with a UIAlertController.
</Matrix-Morpheus-Meme>

Related

Wait when open app after push notification

When user get a notification when the app is in background , than he push the notification message in home screen , than when the app is open this one is being called :
//on delegate class
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
Than here, i want to perform something , than wait for his delegate here, only than to let the app become active again, how can i eliminate the app from starting, only when i am ready for it ?

how to handle push notification if application is in inactive mode?

how do we handle push notification if app is in background mode? means i want to get push notification alert message when I am reopen the application after got the push notification alert.
can I do this ?
I am using this method when my my app is in active mode.
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
}
But when the app is not active, so how to get this?
You don't. Your app is not guaranteed to receive notifications. If the user taps the "open" button then your app will start with the dictionary passed in as the startup parameters in application:didFinishLaunchingWithOptions:. Of course, you users might tap cancel instead.
If the user taps the notification then only trigger to get the notification payload (or) userInfo dictionay value. which using below code in appdelegate file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
}
}

UILocalNotification when app is closed

Using the UILocalNotification when the app is open, this function in the app delegate is fired :
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
but when the app is close and not in the background, it starts the app when i hit the notification massage, but it doesnt fire this method.
i need to fire it because she is the one who take me to another scene-that i need to present when someone get the notification.
it works only when she is on background .
You have to implement application:didFinishLaunchingWithOptions:. The notification will be one of the options.
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
// handle your notification here.
}
}
From the specs:
If the action button is tapped (on a device running iOS), the system
launches the application and the application calls its delegate’s
application:didFinishLaunchingWithOptions: method (if implemented); it passes
in the notification payload (for remote notifications) or the local-notification
object (for local notifications).
In other words, application:didReceiveLocalNotification is only for when, as you've found, the app is running.
If the app is LAUNCHED due to a local (or for that matter remote) notification, the goods from the notification are passed into the application:didFinishLaunchingWithOptions: method, and that's where you catch that.

UILocalNotification

I have UILocalNotification the has two buttons a cancel and view button when the application is in the background and the alert come up I click the view button and it opens on application but I have a method call that is ment to run if the launch option has a UILocalNotification object that is not working
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//notifcation key
UILocalNotification *notifcation = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notifcation) {
NSLog(#"working");
}
}
when your application is in background then didFinishLaunchingWithOptions will not call in that case use -
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
here userInfo is same as UIApplicationLaunchOptionsLocalNotificationKey.
Thanks for Help I have worked it out I thought that the launch options were passed to the application even if the app was in the background but that is not the case if application is in background and a local notification comes in the application delegates has the didReceiveLocalNotification method called I thought that was call when application was in foreground and when in the background application did launch with options was called but the options only has the local notification key when the app is closed (not in background) and the notification is call to start up the application.
so if ay one else has te same issue make sure you use
-(void) application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
method when you click your action button on the local notification alert to run and actions when you open your application from that notification alert button.

Check for waiting notifications

With iOS 5 and the notification center you can have waiting, stacked up notifications each with their own data and message. Is there a way when your program is launched regularly (without tapping on the notification) to discover the waiting notifications and more importantly the data associated with them?
The other question associated with this is when the push notifications come in and your app is in the background does application:didReceiveLocalNotification: still get called or does it just go and wait till the app is launched and then you're expected to manually handle it with the launch data in theapplication:didFinishLaunchingWithOptions:
My scenario is that I need to update some core-data models with the data attached to the push notifications so I want those changes reflected no matter how they launch the app.
From my experience, there is no way to find out about notifications other than launching the app directly through them (on iOS 5, that would be right after receiving the notification or from the queued notifications present at the Notification Center).
An alternate strategy would be to use icon badges.
If you badge your app icon when receiving notifications, you could check for that number every time the app is launched and then perform the required actions.
When a notification pops up you can either view it or cancel. If the user has canceled notification, you can check for them the next time they go in the app:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if( localNotif ) {
// Do some stuff
application.applicationIconBadgeNumber = 0; // Reset num of notifications on app icon
}
}
This method gets fired if you receive a notification while you are in that specific app, or you choose to acknowledge a local notification:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
BOOL inApp = [application applicationState] == UIApplicationStateActive;
if( inApp ) { // they got this in the app
//Do some stuff in the app
}
else {
// They acknowledged the notification out of the app and here they are
// Do some other stuff
}
application.applicationIconBadgeNumber = 0;
}
That gives you good coverage for all scenarios with local notifications which would be:
Notification is received, user cancels. Check later in didFinishLaunchingWithOptions
Notification is acknowledged, the app is not open. Check in didFinishLaunchingWithOptions
Notification is acknowledged, the app is in the background. didReceiveLocalNotification:
Notification is presented while in the app didReceiveLocalNotification: