Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data from payload when remote push notification arrive while my app is in background or closed

Tags:

ios

swift

swift3

I need to capture data from payload when recive a remote push notification, on IOS 9 i did this using the func: didReceiveRemoteNotification On IOS 10 using swift 3.0 i've implemented this 2 functions.

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
print("\(notification.request.content.userInfo)")

completionHandler([.alert, .badge, .sound])
}

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
print("User Info = ",response.notification.request.content.userInfo)

completionHandler()
}

the second fuction only executes when the user touch the push

i need capture data when a push notification arrives while my app is in background or even closed.

Regards. greetings

like image 244
Christian Gonzalez Avatar asked Oct 06 '16 15:10

Christian Gonzalez


People also ask

What is push notification payload?

Push payloads are sent with encrypted content when the mobile app supplies an RSA public encryption key upon push registration with the Salesforce push notification service. When a payload is sent from a customer org to a user's device, the mobile app processes and decrypts the payload.

What is payload in push notification IOS?

The payload contains any custom data that you want to send to your app and includes information about how the system should notify the user. You construct this payload as a JSON dictionary and send it as the body content of your HTTP/2 message.

What is push notification payload maximum size?

Android 12(vanilla/stock) Collapsed notification: body - 43 characters(1 line), header - 39 characters(1 line) Extended notification: body - 504 characters(10 lines), header - 79 characters(2 lines) Notification with banner(image): body - 96 characters(2 lines), header -79 characters(2 lines)


1 Answers

When a push notification is received and your app isn't running, you need to implement your notification logic in didFinishLaunchingWithOptions:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
   // Called when a notification is tapped and the app wasn't running, not in foreground neither in background. 
   if let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: AnyObject]{
        // your logic here!
    }
}
like image 61
user3546621 Avatar answered Oct 16 '22 23:10

user3546621