0

I want to implement push notification on my app. I am able to send notification to the device. What I want to do is to check whether users open the app by clicking the alert. If yes, I will popup some dialog or show something different based on the content of the alert. I don't know how to do that in my application. I know there is a method as shown below I can override on AppDelegate class. This method will be called when user receives a notification. But I can't know whether user open the app by clicking the alert or not. How can I achieve it?

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523
  • Alert in the sense, you're mentioning push notification ? When you tap on the push notification, you need to show something different ? Right ?? – Balaji Ramakrishnan Apr 28 '16 at 05:40
  • http://stackoverflow.com/questions/16393673/detect-if-the-app-was-launched-opened-from-a-push-notification may this link will help you – Sheereen S Apr 28 '16 at 05:44

3 Answers3

1

if you press the Alert on APNS on confirmation alert the following delegate will fire

if user press Allow button

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

let trimmedDeviceToken = deviceToken.description .stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "<>"))
    .stringByReplacingOccurrencesOfString(" ", withString: "")
print("Device Token \(trimmedDeviceToken)")

}

if user press Don't Allow button

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Failed to get token, error: \(error)")
}

After that whenerver you recive the Notification the following delegate is called

 func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
   {

       print(userInfo) // you can get the details in here
        if ( application.applicationState == UIApplicationState.Inactive || application.applicationState == UIApplicationState.Background ){
                print("opened from a push notification when the app was on background")
        }else{
                print("opened from a push notification when the app was on foreground")
        }
    }

for sample tutorial see this

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0

check the application state using the following condition, inside didReceiveRemoteNotification method,

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
    {
            if ( application.applicationState == UIApplicationState.Inactive || application.applicationState == UIApplicationState.Background ){
                    //Tapped from a notification and the app is in background.
            }else{
                    //App is in Foreground...
            }
    }

Hope this helps.

RJV Kumar
  • 2,408
  • 1
  • 19
  • 28
-1

Use UIAlertViewDelegate methods by setting tag to your alert view