How can I differentiate between push notifications received in app and touched from outside the app?
Asked Answered
M

4

6

I have push notifications setup in my app. I have the method:

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:     (NSDictionary *)userInfo
 {
      if()
      {
           //app is in foreground to get here
      }
      else if()
      {
           //app is in background and then the notification is clicked, to get here
      }
 }

I need to differentiate between touches of the notification outside the app, and simply receiving the notification in the app. Any help?

Motorman answered 12/7, 2013 at 9:45 Comment(3)
not getting what are you trying.. :(Canaliculus
by outside the app, u mean app is not running and we get notification. you click on that notification and your app runs. And u want to perform something at that very moment?Parotid
Yes exactly, I actually want to go to a specific view.Motorman
P
6
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateActive )
        // app was already in the foreground
    else
        // app was just brought from background to foreground
    ...
}
Parotid answered 12/7, 2013 at 9:55 Comment(5)
This is not answering the questionSweetener
@ManuelM. Then how should I answer the question?Parotid
Because @KKendall wants to know if the user tapped on the notification or it simply came from the apns server. Your solution doesn't solve that question. This solution only determines if the app is in background or not. You'll see that the current state of the app when you open it through the notification is not active. So, it's the same! I've been dealing with this problem and the ideal solution should be using the isHandlingRemoteNotification _applicationFlags. But is private! Damn it!Sweetener
@ManuelM. I am sorry if I am not getting your question properly. But I guess you want to handle 3 app states in that I guess you can check for all 3 states: active, inactive and background. See this if it helps : developer.apple.com/library/ios/documentation/iphone/conceptual/…Parotid
I've tested and UIApplicationStateActive is returned when push message received while app is in foreground.Blinking
L
3

Use Following methods for receiving Push Notification in Foreground as well as Background. In Backgound, You can show Default Alert / Banner But In Foreground, You Can't show Alert / Banner but You can manage it by UIAlertView.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateBackground | application.applicationState == UIApplicationStateInactive )
    {
        // Application is in Background
    }
    else
    {
        // Application is in Foreground
    }
}

It's Works Fine...!!!

Lory answered 12/7, 2013 at 10:50 Comment(0)
B
2
    - (void)applicationWillResignActive:(UIApplication *)application
    {
        applicationIsActive = NO;
    }
    - (void)applicationDidBecomeActive:(UIApplication *)application
    {
       applicationIsActive = YES;
     }

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (applicationIsActive)
{
 }
  else 
 {
  }
}
Benis answered 12/7, 2013 at 10:0 Comment(0)
M
1

This is better handled with UNUserNotificationCenter.

UNUserNotificationCenter.current().delegate = self
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    // a notification arrived while the app was in the foreground and has a value for badge, alert, or sound
    // if the notification also has content-available=1 then didReceiveRemoteNotification will also be called.         

    // we can have iOS to display the alert normally  
    completionHandler([.alert, .sound, .badge])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    // the user took an action (i.e. tapped) on a notification 
    // unless you support custom notification action that run in the background the app is now in the foreground. The tap may have caused the app to launch OR brought it to the foreground OR the app may have already been in the foreground
    completionHandler()
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    // a notification arrived with content-available = 1
    // the application may be in the background or foreground
    // note: if the app is in the foreground AND the notification also has a value for badge, alert, or sound then the userNotificationCenter(willPresent) method will also be called
    completionHandler(.newData)
}
Mickimickie answered 19/3, 2018 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.