Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apple Watch Notification Payload

I want to add values to the apple watch notification (the current screen uses hard coded data):

enter image description here

The values I want to add are for these fields: "Amount", "At" and "When". How can I add get this values from the PushNotificationPayload.apns file and show it on the notification?

This is the PushNotificationPayload.apns file:

{
"aps": {
    "alert": {
        "body": "New Transaction\n\n",
        "title": "Optional title"
    },
    "category": "newTransactionCategory"
},

"WatchKit Simulator Actions": [
                               {
                               "title": "Details",
                               "identifier": "transactionDetailsButtonAction"
                               }
                               ],

"customKey": "Use this file to define a testing payload for your notifications. The aps dictionary specifies the category, alert text and title. The WatchKit Simulator Actions array can provide info for one or more action buttons in addition to the standard Dismiss button. Any other top level keys are custom payload. If you have multiple such JSON files in your project, you'll be able to select them when choosing to debug the notification interface of your Watch App."
}
like image 876
user1872384 Avatar asked Sep 28 '22 10:09

user1872384


1 Answers

These are the steps,

  • Create a new class which is a subclass of WKUserNotificationInterfaceController.

  • From the storyboard, select your Dynamic Interface Scene for notification (if you have not created it, enable 'Has Dynamic Interface' in attribute inspector of your Static Scene ) and in Identity Inspector set custom class as created above.

  • Now modify your PushNotificationPayload.apns file contents as below,

    {
        "aps": {
            "alert": {
                "body": "New Transaction\n\n",
                "title": "Optional title"
            },
            "category": "newTransactionCategory"
        },
    
        "WatchKit Simulator Actions": [
                                       {
                                       "title": "Details",
                                       "identifier": "transactionDetailsButtonAction"
                                       }
                                       ],
    
        "Amount": "USD 20",
        "At": "Mc Donalds",
        "When": "Today",
    }
    
  • When a remote notification is received, this method will be called in your custom notification interface class and you will receive the custom keys in the dictionary 'remoteNotification' which you need to use to set the text of your labels here.

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

    1. Select your targets on the top and select Edit Scheme

    2. Click on duplicate scheme at the bottom and give your custom name like 'NOTIFICATION-Mywatchkitapp' etc...

    3. Then, select WatchKit Interface to Dynamic Notification, Notification Payload to your PushNotificationPayload.apns file and run for this target.

like image 159
Dhaval Panchal Avatar answered Oct 05 '22 08:10

Dhaval Panchal