How to Handle Action Buttons in Push Notifications

11,420

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 {
Share:
11,420

Related videos on Youtube

Brijesh Bhatt
Author by

Brijesh Bhatt

Hi there ! A holistic IT professional with experience and interest in a widespread area of technology. Always interested in something new or unique. Thanks !

Updated on September 08, 2020

Comments

  • Brijesh Bhatt
    Brijesh Bhatt over 3 years

    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.

    • vivekDas
      vivekDas about 9 years
      You cant check push notifications in Simulator.
    • user1872384
      user1872384 almost 9 years
      Hey having the same issue... Did you manage to solve it?
  • vivekDas
    vivekDas about 9 years
    And on clicking on "view button" you will get a callback in the UIApplication delegate method "- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo" there you have to handle the action.
  • user1872384
    user1872384 almost 9 years
    He's not asking for this.... He wants to display a button in the push notification on the apple watch (device). The json sample you've given doesn't have a action button
  • Jeremy Luisetti
    Jeremy Luisetti almost 9 years
    it's exactly what the code is doing. once you've register for the category (step 1) the json in step 2 display the button in my exemple "Repondre" this is the way to display the button on actual device (without the watchkit simulator action, this does'nt work on real device.)
  • user1872384
    user1872384 almost 9 years
    Got it thanks! Your solution works.... got confuse by the "WatchKit Simulator Actions" json provided inside Xcode.
  • Jeremy Luisetti
    Jeremy Luisetti almost 9 years
    No problem.. the "WatchKit Simulator Actions" got me confuse too at first... I have tried WatchKit Actions in my aps server at first beleving that's the way to do it..
  • MayNotBe
    MayNotBe almost 9 years
    @JeremyLuisetti: in the handleActionWithIdentifier method //Do stuff here... what stuff do I need to do to present an interface controller? I tried [self pushControllerWithName...] but thats not an option and reloadRootControllerWithNames ...just opens the watch app, it doesn't go to the particular page I'd like
  • Jeremy Luisetti
    Jeremy Luisetti almost 9 years
    for this i'd use "presentControllerWithName" but warning, the "handleActionWithIdentifier" is executed in main interface controller only...
  • OxenBoxen
    OxenBoxen almost 9 years
    The above apns structure worked for me as well. Don't wory about 'actions' keys or anything, if you set up the category correctly with associated actions, all you need to send is the 'category' key with optional 'title', 'body' keys