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.
Turn on notifications for Android devicesTap More on the bottom navigation bar and select Settings. Tap Turn on notifications. Tap Notifications. Tap Show notifications.
Overview. Use remote notifications (also known as push notifications) to push small amounts of data to devices that use your app, even when your app isn't running. Apps use notifications to provide important information to users. For example, a messaging service sends remote notifications when new messages arrive.
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: [])
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");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With