Search code examples
objective-cpush-notificationapple-push-notificationswatchkitwatchos-2

How to get Notification payload in WatchOS2?


I have implemented Dynamic and Static notification in current Watch App. Even I am able to receive event in - (void)handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)remoteNotification method in case of any action in Dynamic Notification.

  • Now my concern is, how to receive any event/callback on tapping Notification view that actually opens the WatchOS App ?
  • How can I get the Push Notification Payload in any method on launching app by tapping notification ?

I am using the following method to set UI in case of Dynamic Notification interface. but, this will not be the case for Static one.

- (void)didReceiveRemoteNotification:(NSDictionary *)remoteNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler { 
    NSLog(@"%@",remoteNotification);
    completionHandler(WKUserNotificationInterfaceTypeCustom);
}

EDIT

I have added following code in appdelegate to register for Notifications.

UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
        [application registerUserNotificationSettings:settings];
        [application registerForRemoteNotifications];

Solution

  • I would look into the following delegate method, with regard to your Watch App's extension delegate, for the answer to both of your questions:

    - (void)handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)remoteNotification
    

    Perimeters:

    • Identifier: equals "" (empty NSString) if the app is launched without tapping one of the notification buttons, or equal to the identifier you set to the notification action button if the user launches the app by tapping one of the notification's action buttons.
    • remoteNotification: the payload that came with the remote notification. It is in the format of a Dictionary, and so you can get the payload from this object.

    Answer to Question 1: When the user launches the app, if implemented, this method will be called and allow you to reconfigure the app based on the user's selection.

    Answer to Question 2: The payload has the traditional structure of the Apple Notification Payload dictionary. The aps key contains the alert information and the badge information. Then your custom data is attached to the overall dictionary. To access it, if you configure the payload dictionary with a key "testKey", you could access it by using remoteNotification[@"testKey"].

    Update

    According to Apple, the static notification "must not include controls, tables, maps, or other interactive elements," because it is only to be used as a fallback if the interactive notification cannot be launched in a timely manner. Source: Apple watchOS Developer Library.

    Sources and/or Additional Resources:

    Apple Developer: WK InterfaceController Class Reference

    Apple Developer: The Remote Notification Payload

    Apple Developer: Notification Essentials for watchOS