1

I tried this: Detect if the app was launched/opened from a push notification

But the difference is that I have set the key content-available = 1 which means that the didReceiveRemoteNotification event is always executed not only when it is played in the notification center. How can I distinguish when the notification center or icon app is triggered?

Community
  • 1
  • 1
sotocast
  • 72
  • 1
  • 8

2 Answers2

1

I have found the solution, the posting may be useful to someone else. In the didReceiveRemoteNotification fetchCompletionHandler set variable timeReceiveRemoteNotification = NSDate () with the date the notification is received. And in applicationDidBecomeActive I check that the time elapsed between the current date and the execution of the didReceiveRemoteNotification event is less than 1s. Because if the application is launched from the notification center the first method that is executed is didReceiveRemoteNotification then applicationDidBecomeActive and if it is launched from the app icon the event that is executed is only applicationDidBecomeActive and therefore the variable timeReceiveRemoteNotification stays with the assigned value of the Date of receipt of the notification, which was probably more than one second. In applicationDidBecomeActive the code is:  

LaunchIconApp = true
        println (NSDate().TimeIntervalSinceDate (timeReceiveRemoteNotification))
        if(launchIconApp && launchNotification && NSDate().TimeIntervalSinceDate (timeReceiveRemoteNotification) <1)
        {
            println("Comes from the notification center")
            LaunchNotification = false
        } Else {
            println("Comes from the app icon")
        }
sotocast
  • 72
  • 1
  • 8
0

If your app is not open i.e its not in the background. When you open app below method is called :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

You can write this line in the above method.

NSDictionary *remoteNotifiInfo = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

The remoteNotifiInfo will always be not nil if the app is triggered by clicking on notification. If its nil this means that the app is opened by user by tapping on the application icon.

Sneha
  • 1,404
  • 15
  • 24
  • Thanks, what if it's in the background? How do I know if it was launched from icon app or notification center? Thank you for your answer – sotocast Dec 23 '16 at 15:28