1

I am trying to put custom buttons in my IOS 8 application for the PUSH Notifications received in my chat app.

Below is my code but the Push notifications are not showing the ship buttons.

if([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{

    //Defining Ap Actions Categories


    UIMutableUserNotificationAction *replyAction =
    [[UIMutableUserNotificationAction alloc] init];

    // Define an ID string to be passed back to your app when you handle the action
    replyAction.identifier = @"REPLY_ACTION";

    // Localized string displayed in the action button
    replyAction.title = NSLocalizedString(@"REPLY", nil);

    // If you need to show UI, choose foreground
    replyAction.activationMode = UIUserNotificationActivationModeForeground;

    // Destructive actions display in red
    replyAction.destructive = NO;

    // Set whether the action requires the user to authenticate
    replyAction.authenticationRequired = YES;


    UIMutableUserNotificationAction *remindLaterAction =
    [[UIMutableUserNotificationAction alloc] init];

    // Define an ID string to be passed back to your app when you handle the action
    remindLaterAction.identifier = @"REMIND_LATER_ACTION";

    // Localized string displayed in the action button
    remindLaterAction.title = NSLocalizedString(@"REMIND", nil);

    // If you need to show UI, choose foreground
    remindLaterAction.activationMode = UIUserNotificationActivationModeForeground;

    // Destructive actions display in red
    remindLaterAction.destructive = NO;

    // Set whether the action requires the user to authenticate
    remindLaterAction.authenticationRequired = YES;

    // First create the category
    UIMutableUserNotificationCategory *singleChatCategory = [[UIMutableUserNotificationCategory alloc] init];

    // Identifier to include in your push payload and local notification
    [singleChatCategory setIdentifier:@"SINGLE_CHAT"];

    // Add the actions to the category and set the action context
    [singleChatCategory setActions:@[replyAction, remindLaterAction]
                        forContext:UIUserNotificationActionContextDefault];

    // Set the actions to present in a minimal context
    [singleChatCategory setActions:@[replyAction]
                        forContext:UIUserNotificationActionContextMinimal];

    NSSet *categories = [NSSet setWithObjects:singleChatCategory,nil];

    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:categories]];

    [[UIApplication sharedApplication] registerForRemoteNotifications];
} else {

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound)];
}`

I have also put in the localised string file following code.

"SINGLECHAT_WITH_PREVIEW"       =   "%@:%@";
"SINGLECHAT_WITHOUT_PREVIEW"    =   "%@ %@ %@";
"REPLY"                         =   "Reply";
"REMIND"                        =   "Remind Me";

Here is the Push APS details I am getting from server as userInfo as well.

{
    aps =     {
        alert =         {
            category = "SINGLE_CHAT";
            "loc-args" =             (
                "USER DEV12347",
                "TEST NOTIFICATION MESSAGE"
            );
            "loc-key" = "SINGLECHAT_WITHOUT_PREVIEW";
            title = Closrr;
        };
        badge = 1;
        sound = "push_play.aiff";
    };
    from = "+67123456";
    to = "+67890765";
    type = 2;
}

Have I done any mistake ?? Please advice.

Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46
codelover
  • 1,113
  • 10
  • 28

1 Answers1

1

The category should be at the first level of you aps object, not within the alert:

{
    aps =     {
        category = "SINGLE_CHAT";
        alert =         {
(...)

Reference: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW1

jcaron
  • 17,302
  • 6
  • 32
  • 46
  • Ill try this some making the server changes :) – codelover Dec 08 '15 at 04:12
  • Thanks You solved this, such a small mistake , overlooked this :) Thanks JCaron. Appreciate your advice. – codelover Dec 08 '15 at 04:19
  • I've done this and also Closed this issue , but one problem is when the app is in background I can do whatever I want to send XMPP Message but when the App is out of memory, nothing happens. Can you help how to achieve like whatsapp sending message when even not active in memory or not activated to send the reply from notification. – Vicky Dhas just now edit – codelover Dec 08 '15 at 06:47
  • @VickyDhas, not sure I understand your issue, but it looks like a different question, so please create a new question for that. – jcaron Dec 08 '15 at 08:53
  • Please help me - described in another post: http://stackoverflow.com/questions/34152184/swipe-buttons-in-ios-9-remote-notification-work-when-app-in-background-not-whe – codelover Dec 08 '15 at 09:26