Sliding from the lock screen and/or pressing a notification from the notifications page... do both pass from applicationDidFinishLunchingWithOptions when the app is totally closed?
My concern is because, when the app is closed and not in background, whenever I press on the app from the notification, my app opens and goes where it has to go... but whenever I press slide to open, the app opens but it does not go to the page it should go.
The docs say that if the action button on the notification is pressed, it calls application:didFinishLaunchingWithOptions and passes in the notification payload. Later it says if the app is running in the foreground, it delivers the notification via application:didReceiveRemoteNotification:. This implies to me that when the app is backgrounded or not running, then application:didFinishLaunchingWithOptions is called. Otherwise, application:didReceiveRemoteNotification: is called.
application:didFinishLaunchingWithOptions: will only be called if your app is not launched already. While it is true that the options will include info on notifications if that is what ended up launching the app, what you want is to handle your local notification logic here:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
Related
I developed an application and used FCM to handle notifications. When the application is in background and a notification received, by clicking on the notification the application:didReceiveRemoteNotification: is called and everything is ok! but if the app is opened with other ways like tapping on the app icon, the application:didReceiveRemoteNotification: function is not called and the notification stays unread.
I know this is how it works but i want whenever there is a notification and no matter how the app opened the application:didReceiveRemoteNotification: called!
I am having some problems with the behavior push notifications in my app.
If the app is not running (not even in the background), when I touch a notification I can parse its payload and do a custom action, by reading the "launchOptions" dictionary.
When my app is running in the background, I am not being able to detect when a user foregrounds the app by tapping the notification, as application:didReceiveRemoteNotification:fetchCompletionHandler: is called immediately in the background, and when I foreground the app I have no way to check if it was done by tapping the app icon or the notification.
I know this is possible because Facebook's Messenger app does this - if I tap the notification it brings me to the chat, else it brings me to where I was previously.
Thanks.
If the user taps the app icon, application:didReceiveRemoteNotification:fetchCompletionHandler does not get called.
If they tap the notification, application:didReceiveRemoteNotification:fetchCompletionHandler does get called and application.applicationState will be UIApplicationStateInactive.
Check out this answer for a code snippet that you can use to quickly see this in action to have it make more sense.
Also, to achieve your goal be sure to do any updating of views when the application state is UIApplicationStateInactive, not UIApplicationStateBackground. The application state will be UIApplicationStateBackground when the push notification comes in. The application state will be UIApplicationStateInactive only when the user launches the app by tapping the notification.
I've seen a lot of answers with if statements like
if (application.applicationState != UIApplicationStateActive) { ... }
but that won't allow you to distinguish between a push notification being delivered and the user tapping a notification.
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.
According to didReceiveRemoteNotification when in background , we used to be able to handle the user opening the app by clicking the action button on a push notification (or swiping on the push notification, depending on how the user sees push notifications) by implementing -application:didReceiveRemoteNotification: and then checking inside the method whether the application's applicationState was not active.
In iOS 7, there's the new remote-notification background mode, which allows the app to perform background fetch when a remote notification is displayed to the user (without the user necessarily doing anything to the notification). To support this mode, you are supposed to implement the -application:didReceiveRemoteNotification:fetchCompletionHandler: method.
The documentation for -application:didReceiveRemoteNotification: says that if your application delegate implements the application:didReceiveRemoteNotification:fetchCompletionHandler: method, then "the app object calls that method instead of this one." Which means we cannot use -application:didReceiveRemoteNotification: to handle remote notifications anymore, since it's not going to be called.
We should probably put handling logic in application:didReceiveRemoteNotification:fetchCompletionHandler:, but the previous trick for handling it doesn't make sense anymore -- previously, we depended on the fact that the only way for -application:didReceiveRemoteNotification: to be called when the app is not active was if the user tapped the action button on the notification to open the app. However, now, the whole point of the remote-notification background mode is that it can call application:didReceiveRemoteNotification:fetchCompletionHandler: in the background every time a remote notification is received, before the user does anything to it.
So then, how can we now tell when the user opens the app using the action button on the notification?
You still check the application state in application:didReceiveRemoteNotification:fetchCompletionHandler:
UIApplicationStateBackground - App is in the background receiving a push notification
UIApplicationStateInactive - App is opening from the user tapping a notification
I was use this delegate function to add the 『Notification Number』.
Cause our Server not send the Badge to our Clients.
Then I used the strange method to add the 『Notification Number』 with this delegate function, and I also add a code to switch UIViewController in this function.
I found out when I use the server push Notification to my test App, and the status of test App is in the background, even I am using Twitter or Safari.
My test App also switch UIViewController to another UIViewController after I push Notification from the server.
I've implemented
application:didReceiveRemoteNotification:
to store data in my app when a push notification is received.
However when my app is in background and I receive a notification, the data is stored only if I touch the notification banner appearing on top:
Instead, if I touch the app icon to reopen it, the content of the notification is not stored:
Since I'm receiving the notifications only when I use the distribution profile, I'm not sure if application:didReceiveRemoteNotification: is invoked only when I push the notification banner on top.
I thought it is always invoked at the time a notification is received, and not after a user action on the device.
UPDATE.
I don't know if this can help but, just to let you know, I haven't implemented any of these methods:
– applicationDidEnterBackground:
– applicationWillEnterForeground:
- applicationDidBecomeActive:
I think I've found out why. From documentation:
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.
http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html
However, I'm wondering if there is a way to load the payload even if the app has been re-opened by touching the icon.