Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if application: didReceiveRemoteNotification: fetchCompletionHandler: was called by tapping on a notification in Notification Center

application: didReceiveRemoteNotification: fetchCompletionHandler: 

is different from

application: didReceiveRemoteNotification: 

How? from the docs:

Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running, the system calls this method regardless of the state of your app. If your app is suspended or not running, the system wakes up or launches your app and puts it into the background running state before calling the method. If the user opens your app from the system-displayed alert, the system calls this method again so that you know which notification the user selected.

My struggle is: I want to know if the method was called by the user tapping an a system-displayed alert from the Notification Center or from a silent push notification that wakes up the device. Currently, as far as I can see, there is no obvious way to differentiate.

- (BOOL)application: didFinishLaunchingWithOptions: 

Tracking the launchOptions in the above method is not a solution because it's only called if the app is suspended/not running in background. If it's running in the background it doesn't get called.

like image 913
Devfly Avatar asked Apr 19 '14 09:04

Devfly


People also ask

What is mobile app push notification?

What are push notifications? A push notification is a message that pops up on a mobile device, such as a sports score, an invitation to a flash sale or a coupon for downloading. App publishers can send them at any time, since users don't have to be in the app or using their devices to receive them.

Can apps read notifications?

Built-in voice Notifications for Android phonesTalkback will read out loud notifications as they arrive along with everything on your screen (not just notifications). However, this changes the way you interact with your phone, which might be a drawback if you are looking for app to only read out loud notifications.

What is real time push notification?

Real-Time Notifications (RTN) are server-to-server push notifications that provide comprehensive data about in-app purchases in real time. You can use this information to monitor purchase state changes associated with your customer's in-app purchasable items.


2 Answers

The Apple docs are a bit confusing

application: didReceiveRemoteNotification: fetchCompletionHandler:   

is used if your application supports the remote-notification background mode (ie you're doing BackgroundFetch.)

application: didReceiveRemoteNotification:   

is called when the OS receives a RemoteNotification and the app is running (in the background/suspended or in the foreground.)
You can check the UIApplicationState to see if the app was brought to foreground by the user (tapping on notification) or was already running when notification comes in.

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {       UIApplicationState state = [application applicationState];         // user tapped notification while app was in background     if (state == UIApplicationStateInactive || state == UIApplicationStateBackground) {          // go to screen relevant to Notification content     } else {          // App is in UIApplicationStateActive (running in foreground)          // perhaps show an UIAlertView     } } 
like image 98
race_carr Avatar answered Sep 17 '22 19:09

race_carr


You could check the UIApplication's applicationState to distinguish silent calls from calls made with the application being actively used by the user:

typedef enum : NSInteger {    UIApplicationStateActive,    UIApplicationStateInactive,    UIApplicationStateBackground } UIApplicationState; 

Or keep your own flag set on the delegate's applicationDidEnterBackground:.

like image 40
Rivera Avatar answered Sep 19 '22 19:09

Rivera