Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

ios - How to Handle Action Buttons in Push Notifications

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

enter image description here

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 {

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...