I have tried adding in Actionable Notifications for my Parse app, iPrayed 4 U. In the app, someone can "Pray" for you by clicking a button. Doing so runs a Cloud Code that includes a category for the APNS payload. In my AppDelegate I have:
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
}
}];
}
So, I run to testing. When someone prays for me, I get the notification on my iPhone, drag down and there is the "Say Thanks" button. I click it, but nothing happens. Here is the kicker. Normally I would say, "Well, I messed something up, start debugging". However, I also have an Apple Watch. When I click the Say Thanks button attached to the notification on my Watch, it sends the Push, vs. doing nothing when I click the same button on my iPhone.
Any thoughts as to what is going on here?
UPDATE:
In console, when it fails, I get:
[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.
Eventually I get a success call, but by that time it still doesn't actually send the Push. Any idea why it is happening only when tapped from iPhone and not watch?
Because you call completionHandler()
before PFCloud
finished. You need call completionHandler()
in completionBlock
Do this.
- (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();
}
}
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