Payload

I want to add values ​​to Apple's watch notification (the current screen uses hard-coded data):

enter image description here

The values ​​I want to add relate to these fields: Amount, B, and When. How to add these values ​​from the PushNotificationPayload.apns file and show it in 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." } 
+5
source share
1 answer

These are the steps

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

  • From the storyboard, select your dynamic interface script for notification (if you did not create it, enable the "Dynamic Interface" in the attribute inspector of your static scene), and in the Identity Inspector set your own class created above.

  • Now modify the contents of the PushNotificationPayload.apns file as shown 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 you receive a remote notification, this method will be called in your user class of the notification interface, and you will receive user keys in the "remoteNotification" dictionary, which you need to use to set the text of your shortcuts here.

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

    • Select the goals at the top and select "Edit Schema"

    • Click on the double chart below and enter your name under the name "NOTIFICATION-Mywatchkitapp" etc.

    • Then select “WatchKit Interface” for dynamic notification, “Notification Payload” in the PushNotificationPayload.apns file and run for this purpose.

+4
source

Source: https://habr.com/ru/post/1216502/


All Articles