'How can I open app from remote notification action button
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.
Solution 1:[1]
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");
}
}
Solution 2:[2]
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: [])
Solution 3:[3]
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])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Sarp Ba?araner |
| Solution 3 | Salar Soleimani |

