Ios 操作按钮不会显示在通知中
我正在尝试将iOS8操作按钮添加到已在工作的通知中。但他们不会出现。这只是一个没有按钮的普通通知Ios 操作按钮不会显示在通知中,ios,ios8,apple-push-notifications,Ios,Ios8,Apple Push Notifications,我正在尝试将iOS8操作按钮添加到已在工作的通知中。但他们不会出现。这只是一个没有按钮的普通通知 //Action UIMutableUserNotificationAction* loginAction = [[UIMutableUserNotificationAction alloc] init]; loginAction.identifier = @"loginAction"; //ID string to be passed back to your app when you handl
//Action
UIMutableUserNotificationAction* loginAction = [[UIMutableUserNotificationAction alloc] init];
loginAction.identifier = @"loginAction"; //ID string to be passed back to your app when you handle the action
loginAction.title = @"login";
loginAction.activationMode = UIUserNotificationActivationModeForeground;
loginAction.destructive = NO;
loginAction.authenticationRequired = YES;
//Category
UIMutableUserNotificationCategory* loginCategory = [[UIMutableUserNotificationCategory alloc] init];
loginCategory.identifier = @"loginCategory"; //ID to include in your push payload
[loginCategory setActions:[NSArray arrayWithObjects:loginAction, nil] forContext:UIUserNotificationActionContextDefault];
//Register Settings and Notification
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:[NSSet setWithObject:loginCategory]]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
使用此代码与将categories参数设置为nil具有相同的效果
有效载荷如下所示:
'{"aps":{"alert":{"body":"text"},"category":"loginCategory"}}'
我刚刚将category键添加到有效负载中
"aps" : {
"alert" : "Pull down to interact.",
"category" : "ACTIONABLE"
}
我遗漏了什么?这是一个非常简单的例子,在Objective-C中,介绍了如何注册具有2个操作的通知
NSString * const NotificationCategoryIdent = @"ACTIONABLE";
NSString * const NotificationActionOneIdent = @"ACTION_ONE";
NSString * const NotificationActionTwoIdent = @"ACTION_TWO";
- (void)registerForNotification {
UIMutableUserNotificationAction *action1;
action1 = [[UIMutableUserNotificationAction alloc] init];
[action1 setActivationMode:UIUserNotificationActivationModeBackground];
[action1 setTitle:@"Action 1"];
[action1 setIdentifier:NotificationActionOneIdent];
[action1 setDestructive:NO];
[action1 setAuthenticationRequired:NO];
UIMutableUserNotificationAction *action2;
action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setActivationMode:UIUserNotificationActivationModeBackground];
[action2 setTitle:@"Action 2"];
[action2 setIdentifier:NotificationActionTwoIdent];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:NO];
UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:NotificationCategoryIdent];
[actionCategory setActions:@[action1, action2]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObject:actionCategory];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings;
settings = [UIUserNotificationSettings settingsForTypes:types
categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
要发送这种类型的通知,只需将类别添加到有效负载中
"aps" : {
"alert" : "Pull down to interact.",
"category" : "ACTIONABLE"
}
当用户从推送通知中选择操作时,将在后台调用这些方法
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {
if ([identifier isEqualToString:NotificationActionOneIdent]) {
NSLog(@"You chose action 1.");
}
else if ([identifier isEqualToString:NotificationActionTwoIdent]) {
NSLog(@"You chose action 2.");
}
if (completionHandler) {
completionHandler();
}
}
希望这有帮助…干杯 在这里查看我的答案以了解详细信息:谢谢,它现在可以工作了。您必须将类别键添加到aps字典中,而不是在它之后或警报字典中。同时,移动到一侧也不会显示按钮。我必须把手指向下移动。移动到一边确实会显示按钮。看看操作系统邮件通知哦,是的,在锁屏上。但在最上面的通知中,它不会。