How can I open app from remote notification action button
Asked Answered
S

3

7

As in subject, I'm trying to open app when user taps 'Accept' button on remote notification.

Below is listed AppDelegate method which is responsible for handling button action:

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

    if ([identifier isEqualToString: @"ACCEPT_IDENTIFIER"]) {

    }

    completionHandler();
}

I was looking for solution awhile but I can't find helpful information for me.



Update: Because sentence 'Actionable notification buttons' cause confusion, img below shows what I mean by this sentence.

enter image description here

Stigmatic answered 8/1, 2016 at 13:29 Comment(4)
what accept button on local notification? cOuld you be more specific ?Madalena
As we know we can define remote notification action buttons, one of my button is named 'Accept'.Stigmatic
isnt the app open when u tap on notification . I think this is the default behaviour for the push notifications.Madalena
yes when you tap into notification itself apps will open, but if you tap into notification actionable button it doesn't open the app by defaultStigmatic
L
5

When you register the device for notifications you do it with a UIUserNotificationSettings:

[[UIApplication sharedApplication] registerUserNotificationSettings:[self createUserNotificationSettings]];

For instance, in this method you can create the UIUserNotificationAction which are the action buttons and custom settings:

- (UIUserNotificationSettings *) createUserNotificationSettings {
UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
[action1 setActivationMode: UIUserNotificationActivationModeForeground];
[action1 setTitle:@"Title1"];
[action1 setIdentifier:@"first_button"];
[action1 setDestructive:YES];
[action1 setAuthenticationRequired:NO];

UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setActivationMode: UIUserNotificationActivationModeForeground];
[action2 setTitle:@"Title2"];
[action2 setIdentifier:@"second_button"];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:NO];

UIMutableUserNotificationCategory *actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:@"ACTIONABLE"];
[actionCategory setActions:@[action1, action2]
                forContext:UIUserNotificationActionContextDefault];

NSSet *categories = [NSSet setWithObject:actionCategory];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
                                UIUserNotificationTypeSound|
                                UIUserNotificationTypeBadge);

return [UIUserNotificationSettings settingsForTypes:types categories:categories];
 }

When you receive the notification the following method gets called and you can check which button was pressed:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)notification  completionHandler: (void (^)())completionHandler {
    if ([identifier isEqualToString: @"first_button"]) {
        NSLog(@"First notification button was pressed");
    } else {
        NSLog(@"Second notification button was pressed");
    }
}
Luminary answered 8/1, 2016 at 14:43 Comment(4)
This is not answer for my question. Implementation presented in your answer has been made by me and it works great, but my question was how can I bring application to foreground using 'handleActionWithIdentifier' method according to clicked button?Stigmatic
I think this is what you're looking for: developer.apple.com/library/ios/documentation/UIKit/Reference/…Tomfoolery
Yup, you have right I didn't read your code carefully. Thanks man!Stigmatic
I edited the code to change the UIUserNotificationActivationMode type since i wrote for background on the answer. I didn't read your question correctly but it is edited now (:Tomfoolery
G
19

Swift 4:

Just make sure you include .foreground to your UNNotificationAction object.

Example:

// This will cause the app to launch in the foreground:
let action = UNNotificationAction(identifier: "showAction", title: "Show", options: [.foreground])

// This will not:
let action = UNNotificationAction(identifier: "showAction", title: "Show", options: [])
Giacobo answered 11/10, 2018 at 9:27 Comment(0)
L
5

When you register the device for notifications you do it with a UIUserNotificationSettings:

[[UIApplication sharedApplication] registerUserNotificationSettings:[self createUserNotificationSettings]];

For instance, in this method you can create the UIUserNotificationAction which are the action buttons and custom settings:

- (UIUserNotificationSettings *) createUserNotificationSettings {
UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
[action1 setActivationMode: UIUserNotificationActivationModeForeground];
[action1 setTitle:@"Title1"];
[action1 setIdentifier:@"first_button"];
[action1 setDestructive:YES];
[action1 setAuthenticationRequired:NO];

UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setActivationMode: UIUserNotificationActivationModeForeground];
[action2 setTitle:@"Title2"];
[action2 setIdentifier:@"second_button"];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:NO];

UIMutableUserNotificationCategory *actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:@"ACTIONABLE"];
[actionCategory setActions:@[action1, action2]
                forContext:UIUserNotificationActionContextDefault];

NSSet *categories = [NSSet setWithObject:actionCategory];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
                                UIUserNotificationTypeSound|
                                UIUserNotificationTypeBadge);

return [UIUserNotificationSettings settingsForTypes:types categories:categories];
 }

When you receive the notification the following method gets called and you can check which button was pressed:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)notification  completionHandler: (void (^)())completionHandler {
    if ([identifier isEqualToString: @"first_button"]) {
        NSLog(@"First notification button was pressed");
    } else {
        NSLog(@"Second notification button was pressed");
    }
}
Luminary answered 8/1, 2016 at 14:43 Comment(4)
This is not answer for my question. Implementation presented in your answer has been made by me and it works great, but my question was how can I bring application to foreground using 'handleActionWithIdentifier' method according to clicked button?Stigmatic
I think this is what you're looking for: developer.apple.com/library/ios/documentation/UIKit/Reference/…Tomfoolery
Yup, you have right I didn't read your code carefully. Thanks man!Stigmatic
I edited the code to change the UIUserNotificationActivationMode type since i wrote for background on the answer. I didn't read your question correctly but it is edited now (:Tomfoolery
P
5

swift 5:

let action = UNNotificationAction(identifier: "sample", title: "sample", options: [UNNotificationActionOptions.foreground])

the UNNotificationActionOptions.foreground gives you the application launch in the foreground and your code runs even the app is force closed.

after creating your action. assign it to the category.

let category = UNNotificationCategory(identifier: "sample", actions: [action], intentIdentifiers: [], options: [])

then assign the category

UNUserNotificationCenter.current().setNotificationCategories([category])
Paddle answered 10/3, 2020 at 4:55 Comment(1)
Thank you! I have been looking for the .foreground option for over two hours (of course I didn't know that it was what I was looking for)Gisellegish

© 2022 - 2024 — McMap. All rights reserved.