How to Handle Action Buttons in Push Notifications

2020-02-02 12:30发布

问题:

Working on Apple Watch Notifications:- so if i add button in Notification Interface (from object Lib) then the error is :

Buttons are not Supported in Notification Interface

PushNotificationPayload.apns has the WatchKit Simulator Actions Like This :

"WatchKit Simulator Actions": 
[
    {
        "title": "View",
        "identifier": "firstButtonAction"
    }
],

and the Simulator Shows this to me

Now my Question is, How can I Handle this View Button when send a PushNotification from server,

If the aps file contain the action button is the only option for Apple Watch,

How to send it from server in Notification Dictionary with specified Key?

How to Change the Action button BG Color?

Will anyone please give me the sample aps file that include ActionButton for Device the Apple Watch not for the Simulator

I just test by changing the WatchKit Simulator Actions Key to WatchKit Actions Key but it shows no Action Button.

As suggested by @vivekDas in Answer, I checked by replacing in the aps as :

"alert" : {
     "body" : "Acme message received from Johnny Appleseed",
     "action-loc-key" : "VIEW",
     "action" : [
        {
           "id" : "buttonTapped",
           "title" : "View"
        }
     ]
  }

but simulator in Xcode does show an action button.

I think this may run on Device Apple Watch, is this...?

What would you suggest to me on this.

回答1:

I've been struggling with this for two Hours, so this is how I do for notifications buttons in production, via real APNS Serv,

1) Register for the category in your appDelegate's parent app :

- (void)registerSettingsAndCategories {
    // Create a mutable set to store the category definitions.
    NSMutableSet* categories = [NSMutableSet set];

    // Define the actions for a meeting invite notification.
    UIMutableUserNotificationAction* acceptAction = [[UIMutableUserNotificationAction alloc] init];
    acceptAction.title = NSLocalizedString(@"Repondre", @"Repondre commentaire");
    acceptAction.identifier = @"respond";
    acceptAction.activationMode = UIUserNotificationActivationModeForeground; //UIUserNotificationActivationModeBackground if no need in foreground.
    acceptAction.authenticationRequired = NO;

    // Create the category object and add it to the set.
    UIMutableUserNotificationCategory* inviteCategory = [[UIMutableUserNotificationCategory alloc] init];
    [inviteCategory setActions:@[acceptAction]
                    forContext:UIUserNotificationActionContextDefault];
    inviteCategory.identifier = @"respond";

    [categories addObject:inviteCategory];

    // Configure other actions and categories and add them to the set...

    UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:
                                            (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound)
                                                                             categories:categories];

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}

2) From your Apns server add the category (for me "respond")

{"aps":{"alert":"bla","category":"respond","badge":2}}

3) In your WatchKitExtention, you have the data passed in :

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

     if ([identifier isEqualToString:@"respond"]) {
//Do stuff Here to handle action... 
     }
}

4) In your Parent app's appDelegate :

- (void) application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
     forRemoteNotification:(NSDictionary *)userInfo
         completionHandler:(void (^)())completionHandler {
    completionHandler();
}

Warning ! you'll have to handle this action too in your Parent app ( because the respond button will be visible too on iphone when you pan down the notification. in the :

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {


回答2:

Use Json Payload like given below

{
    "aps": {
         "badge": 10,
         "alert": "Your message",
         "sound": "cat.caf"
    }
}

Check this apple doc link https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html

The Notification Payload section.