我想在收到推送通知的用户单击通知警报本身上的自定义按钮后,向Web服务发送自动简单的请求。实际上,只有在应用程序位于前景中或将通知操作设置为:
时,它才有效UNNotificationActionOptionForeground
但是我不想在单击按钮后打开应用程序,所以我需要的选项是:
UNNotificationActionOptionNone
通知的配置示例:
-(void)register_notification{
UNNotificationAction *Send_no_open_Action = [UNNotificationAction actionWithIdentifier:@"ACTION_NO_OPEN"
title:@"Send request without open"
options:UNNotificationActionOptionNone];
UNNotificationAction *Send_open_Action = [UNNotificationAction actionWithIdentifier:@"ACTION_OPEN"
title:@"Send request opening app"
options:UNNotificationActionOptionForeground];
NSArray *notificationActions = @[ Send_no_open_Action, Send_open_Action ];
UNNotificationCategory* generalCategory = [UNNotificationCategory
categoryWithIdentifier:@"GENERAL"
actions:notificationActions
intentIdentifiers:@[]
options:UNNotificationCategoryOptionNone];
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center setNotificationCategories:[NSSet setWithObjects:generalCategory, nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
检测类型:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
NSString *Category_identifier=[NSString stringWithFormat:@"%@",response.notification.request.content.categoryIdentifier];
NSString *Action_identifier=[NSString stringWithFormat:@"%@",response.actionIdentifier];
if([Action_identifier isEqualToString:@"ACCEPT_ACTION"]){
[self nsurl_try];
}
if([Action_identifier isEqualToString:@"DECLINE_ACTION"]){
[self nsurl_try];
}
}
我使用的是NSURlsession,因为这是Apple文档的建议:
-(void)nsurl_try{
NSLog(@"NSURL START");
// 1
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",PATHSERVER,@"test_insert.php"]];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
// 2
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = @"POST";
// 3
NSDictionary *dictionary = @{@"email": @"",@"password": @""};
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary
options:kNilOptions error:&error];
if (!error) {
// 4
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
fromData:data completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
// Handle response here
NSLog(@"response %@",response);
}];
// 5
[uploadTask resume];
}
}
这可能是通知有效载荷:
aps, value: {
alert = {
body = "notification body";
title = "notification title";
};
category = GENERAL;
"content-available" = 1;
}
即使使用选项nsurl_try
,我也无法理解要从UNNotificationActionOptionNone
获得响应所缺少的内容。
使用此选项,代码将在NSLog(@"NSURL START");
处停止。
有人可以帮助我吗?