0

I want to present a particular viewcontroller when the user tap on a banner notification OR "OK" on the alertnotification. I am notified whenever the app receives a voip push notification with this method:

func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
    let data = payload.dictionaryPayload
    let aps = data["aps"] as! [String: AnyObject]
    let alert = aps["alert"] as! [String: AnyObject]

    let notification = UILocalNotification()
    //setup the notification
    notification.alertBody = alert["body"] as? String
    notification.alertTitle = alert["title"] as? String
    notification.soundName = aps["sound"] as? String
    notification.alertAction = alert["action-loc-key"] as! String

    //show the notification
   UIApplication.sharedApplication().presentLocalNotificationNow(notification)

How can i add an action to the tapping of the banner or the notification.alertAction ?

KML
  • 2,302
  • 5
  • 26
  • 47

2 Answers2

1

implement the following method:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
  print("applicationDidReceiveLocalNotification")
}

this should get called when you open the app by tapping on the notification banner... within this method you can then present the viewcontroller you want to open...

André Slotta
  • 13,774
  • 2
  • 22
  • 34
  • I can't get an alertcontroller to show when the app is in the background, it only works, albeit as expected, when the app is in the foreground.. – KML Nov 14 '15 at 16:33
  • so... when the app is not in the foreground... what happens then? do you even get a banner or something? – André Slotta Nov 14 '15 at 16:43
  • Well the push notification is received, I know that, because I print it from didReceiveIncomingPushWithPayload to the console when it is received, but it is not possible to run the alertcontroller from the background, so no alert is generated. The alert is not shown. – KML Nov 14 '15 at 16:49
  • 1
    There is no way to do it without user interaction, correct. But I am able to create the desired notification as in my questions. I am just wondering how to create an action, for the action button. I think this tutorial might explain it. http://www.appcoda.com/local-notifications-ios8/ – KML Nov 14 '15 at 16:59
  • did you register for local notifications? – André Slotta Nov 14 '15 at 17:10
  • Yes, notifications work great, I just need to know how to define an action for tapping the notification.. – KML Nov 14 '15 at 17:17
  • right now when you tap the notification the app gets opened, right? – André Slotta Nov 14 '15 at 17:17
  • Works great, but why? is a locat Notification the same as voip push notification from a "remote" server? Or does it have nothing to do with that? – KML Nov 14 '15 at 17:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95142/discussion-between-andre-slotta-and-karlml). – André Slotta Nov 14 '15 at 17:28
0

You need to use local notification with interactive notification.

Apple allow us to use interactive notification in ios8+.And using this you can able to present your view controller on tap of the button created using interactive notification functionality.

For the reference you can use this http://www.appcoda.com/local-notifications-ios8/ and How to implement interactive notifications ios8 and also there are some other useful tutorials for this functionality.

Community
  • 1
  • 1
iDeveloper
  • 607
  • 5
  • 25