I'm trying to handle all possible cases with remote notifications.
I'm ok when app in foreground - didReceiveRemoteNotification is called.
The problem is when app in background state, and I receive push notification.
Nothing is called.
how to let user know that he has new remote notification when app come back to foreground ?
The only way for you to intercept a push notification is when the user tap the notify in the notification center (or when slide the app icon from the lock screen).
In this case before the app go in foreground the didFinishLaunchingWithOptions method in the app delegate is called. You should use the NSDictionary launchOptions to determine if the app has been launched from notification center or by tapping the icon (the normal use)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *pushDic = [launchOptions objectForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"];
if (pushDic != nil) {
NSLog(#"Notification");
}
else {
}
}
Just have a look into the programming guide:
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).
Of course, if your app is in background, there will be nothing called...
If your app is not launched (not even suspended in background), the
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
will contain the notification payload (key UIApplicationLaunchOptionsRemoteNotificationKey):
NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
Related
while the app is in background didReceiveLocalNotification is not called.
So I try to get the notification from didFinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
//...
}
But My App is with Background Mode enable (using external accessory communication)
When click on the notification, didFinishLaunchingWithOptions is not called.
Any other way to retrieve notification ?
By checking Apple's document about notifications, it says:
iOS Note: In iOS, you can determine whether an application is launched
as a result of the user tapping the action button or whether the
notification was delivered to the already-running application by
examining the application state. In the delegate’s implementation of
the application:didReceiveRemoteNotification: or
application:didReceiveLocalNotification: method, get the value of the
applicationState property and evaluate it. If the value is
UIApplicationStateInactive, the user tapped the action button; if the
value is UIApplicationStateActive, the application was frontmost when
it received the notification.
As far as I know, when your app is in background-running state, and there comes a local notification, you won't receive any method call, the notification will be displayed to user, but if user tap the notification and thus reactive your app, you will receive -didReceiveLocalNotification: call.
Here is the situation that I want to handle quoted from Apple's documentation.
As a result of the presented notification, the user taps the action button of the alert or taps (or clicks) the application icon.
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).
If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification . If the application icon is clicked on a computer running OS X, the application calls the delegate’s applicationDidFinishLaunching: method in which the delegate can obtain the remote-notification payload.
How do I handle this situation if there is no information about the notification?
If I understand you correctly, it sounds like you have a UILocalNotification that has been fired, but you need to still handle it if the user taps the application icon instead of the notification. Correct?
If this is the case, then to my knowledge you won't be able to handle the notification from the app delegate, because the app is not being launched or brought out of the background by the notification, but instead by the user's interaction.
However, if you are setting a badgeNumber on the application with the notification then you could try something like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
} else if ([UIApplication sharedApplication].applicationIconBadgeNumber > 0) {
// Assume that user launched the app from the icon with a notification present.
}}
You may also have to check the badgeNumber in - (void)applicationDidBecomeActive:(UIApplication *)application as well.
Improve to #Aron Crittendon answer:
Consider also to handle that in applicationDidBecomeActive:
-(void)applicationDidBecomeActive:(UIApplication *)application
{
if ([UIApplication sharedApplication].applicationIconBadgeNumber > 0) {
//application is in background, fired notification and user tapped app icon with badge
}
}
As the documentation states, if you tap the icon on iOS (and not the notification's alert/banner) then the same method is called but you get no notification information. There is no way to handle a local notification simply by tapping the app icon.
I have a requirement for an application, when it has multiple notifications and when the user clicks one of those, the rest of the notifications of the same app should still be visible in the notification centre. Normally what happens is those other notifications also get cleared. Is there a way that I can bypass this scenario?
Update - Is there a way that we can access received push notifications in the notification center?
Its done by iOS. Programmers have no control over it.
You can get the notification if the user click on the item in notification center and application is opening from it. Following is the code for it
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
//your notification will be in the dictionary if application is opening from notification center
return YES;
}
I would like to know if there is a way to know if an app (which can be closed or open in background) has been launched with a click on:
a notification (in the notification center) ?
or the app icon on the springboard ?
Thanks !!
put this code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
// launched from notification
} else {
// from the springboard
}
}
in your UIApplicationDelegate.
From Apple Docs on Scheduling, Registering, and Handling Notifications
:
iOS Note: In iOS, you can determine whether an application is launched as a result of the user tapping the action button or whether the notification was delivered to the already-running application by examining the application state. In the delegate’s implementation of the application:didReceiveRemoteNotification: or application:didReceiveLocalNotification: method, get the value of the applicationState property and evaluate it. If the value is UIApplicationStateInactive, the user tapped the action button; if the value is UIApplicationStateActive, the application was frontmost when it received the notification.
I'm trying to perform some task when a user gets a push notification and presses VIEW. I don't want the app to just launch like it normally does, I want to perform some other task.
What method gets called when the user gets a push and presses VIEW?? didFinishLaunchingWithOptions doesn't seem to get called (not on iPhone 4 anyways).
I basically want something to happen when a user presses VIEW, and only when they press VIEW.
Thanks.
Are you sure application:didFinishLaunchingWithOptions: is not being called? Normally you can tell that your app was launched from the user clicking on the view button of a remote push notification by inspecting the launchOptions parameter.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *pushInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (pushInfo)
{
// app was launched from a remote push notification
}
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification