use*_*452 9 iphone apple-push-notifications ios parse-platform apple-watch
我已经尝试为我的Parse应用程序添加可操作通知,iPrayed 4 U.在应用程序中,有人可以通过单击按钮为您"祷告".这样做会运行包含APNS有效负载类别的Cloud Code.在我的AppDelegate中,我有:
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIMutableUserNotificationAction *acceptAction =
[[UIMutableUserNotificationAction alloc] init];
acceptAction.identifier = @"THANKS_IDENTIFIER";
acceptAction.title = @"Say Thanks";
// Given seconds, not minutes, to run in the background
acceptAction.activationMode = UIUserNotificationActivationModeBackground;
acceptAction.destructive = NO;
acceptAction.authenticationRequired = NO;
UIMutableUserNotificationCategory *inviteCategory =
[[UIMutableUserNotificationCategory alloc] init];
inviteCategory.identifier = @"TAGGED_CATEGORY";
[inviteCategory setActions:@[acceptAction]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObjects:inviteCategory, nil];
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:types categories:categories];
[[UIApplication sharedApplication]
registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)notification
completionHandler:(void (^)())completionHandler {
if ([identifier isEqualToString:@"THANKS_IDENTIFIER"]) {
[self handleAcceptActionWithNotification:notification];
NSLog(@"Handled");
}
// Must be called when finished
completionHandler();
}
-(void) handleAcceptActionWithNotification:(NSDictionary *)notification {
NSLog(@"SendingThanks");
PFUser *me = [PFUser currentUser];
NSString *theirname = me[@"additional"];
NSString *myAlertString=notification[@"loc-args"];
NSLog(@"%@", notification);
[PFCloud callFunctionInBackground:@"thankYou"
withParameters:@{@"recipientId": myAlertString, @"theirName": theirname}
block:^(NSString *success, NSError *error) {
if (!error) {
// Push sent successfully
}
}];
}
Run Code Online (Sandbox Code Playgroud)
所以,我跑去测试.当有人为我祈祷时,我会在我的iPhone上收到通知,向下拖动并显示"Say Thanks"按钮.我点击它,但没有任何反应.这是踢球者.通常我会说,"好吧,我弄乱了一些东西,开始调试".不过,我也有Apple Watch.当我单击Watch上通知附带的Say Thanks按钮时,它会发送Push,而不是在我点击iPhone上的相同按钮时什么都不做.
有什么想法在这里发生了什么?
更新:
在控制台中,当它失败时,我得到:
[Error]: The request timed out. (Code: 100, Version: 1.8.2)
2015-10-08 13:42:49.424 iPrayed[2231:712503] [Error]: Network connection failed. Making attempt 1 after sleeping for 1.463493 seconds.
Run Code Online (Sandbox Code Playgroud)
最终我获得了成功通话,但到那时它仍然没有实际发送推送.知道为什么只有从iPhone上轻拍而不是观看时才会发生这种情况吗?
因为你completionHandler()还没完成就打电话PFCloud了。你需要打电话completionHandler()电话completionBlock
做这个。
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)notification completionHandler:(void (^)())completionHandler {
if ([identifier isEqualToString:@"THANKS_IDENTIFIER"]) {
PFUser *me = [PFUser currentUser];
NSString *theirname = me[@"additional"];
NSString *myAlertString=notification[@"loc-args"];
[PFCloud callFunctionInBackground:@"thankYou" withParameters:@{@"recipientId": myAlertString, @"theirName": theirname} block:^(NSString *success, NSError *error) {
completionHandler();
}];
} else {
completionHandler();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
206 次 |
| 最近记录: |