如何从远程通知操作按钮打开应用程序
How can I open app from remote notification action button
在主题中,我试图在用户点击远程通知上的 'Accept' 按钮时打开应用程序。
下面列出了负责处理按钮操作的AppDelegate
方法:
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)notification completionHandler: (void (^)())completionHandler {
if ([identifier isEqualToString: @"ACCEPT_IDENTIFIER"]) {
}
completionHandler();
}
我一直在寻找解决方案,但找不到对我有用的信息。
更新:
因为'Actionable notification buttons'这句话造成混淆,下面的img显示了我这句话的意思。
当您为通知注册设备时,您可以使用 UIUserNotificationSettings:
[[UIApplication sharedApplication] registerUserNotificationSettings:[self createUserNotificationSettings]];
例如,在此方法中,您可以创建 UIUserNotificationAction,这是操作按钮和自定义设置:
- (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];
}
当您收到通知时,将调用以下方法,您可以检查按下了哪个按钮:
- (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");
}
}
Swift 4:
只需确保将 .foreground
包含到您的 UNNotificationAction 对象中。
示例:
// 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: [])
swift 5:
let action = UNNotificationAction(identifier: "sample", title: "sample", options: [UNNotificationActionOptions.foreground])
UNNotificationActionOptions.foreground 让您在前台启动应用程序,即使应用程序被强制关闭,您的代码也能运行。
创建动作后。将其分配给类别。
let category = UNNotificationCategory(identifier: "sample", actions: [action], intentIdentifiers: [], options: [])
然后分配类别
UNUserNotificationCenter.current().setNotificationCategories([category])
在主题中,我试图在用户点击远程通知上的 'Accept' 按钮时打开应用程序。
下面列出了负责处理按钮操作的AppDelegate
方法:
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)notification completionHandler: (void (^)())completionHandler {
if ([identifier isEqualToString: @"ACCEPT_IDENTIFIER"]) {
}
completionHandler();
}
我一直在寻找解决方案,但找不到对我有用的信息。
更新: 因为'Actionable notification buttons'这句话造成混淆,下面的img显示了我这句话的意思。
当您为通知注册设备时,您可以使用 UIUserNotificationSettings:
[[UIApplication sharedApplication] registerUserNotificationSettings:[self createUserNotificationSettings]];
例如,在此方法中,您可以创建 UIUserNotificationAction,这是操作按钮和自定义设置:
- (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];
}
当您收到通知时,将调用以下方法,您可以检查按下了哪个按钮:
- (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");
}
}
Swift 4:
只需确保将 .foreground
包含到您的 UNNotificationAction 对象中。
示例:
// 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: [])
swift 5:
let action = UNNotificationAction(identifier: "sample", title: "sample", options: [UNNotificationActionOptions.foreground])
UNNotificationActionOptions.foreground 让您在前台启动应用程序,即使应用程序被强制关闭,您的代码也能运行。
创建动作后。将其分配给类别。
let category = UNNotificationCategory(identifier: "sample", actions: [action], intentIdentifiers: [], options: [])
然后分配类别
UNUserNotificationCenter.current().setNotificationCategories([category])