我知道我可以为iOS通知设置操作,例如:
UIMutableUserNotificationAction *acceptAction =
[[UIMutableUserNotificationAction alloc] init];
acceptAction.identifier = @"THANKS_IDENTIFIER";
acceptAction.title = @"View News Page";
// 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:userNotificationTypes
categories:categories];
[[UIApplication sharedApplication]
registerUserNotificationSettings:settings];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
并在aps的有效负载中包含与此处列出的内容匹配的类别,当应用程序收到所述类别的通知时,它会向其添加操作,并且当按下该操作按钮时,在此处执行操作:
- (void)application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)notification
completionHandler:(void (^)())completionHandler {
if ([identifier isEqualToString:@"THANKS_IDENTIFIER"]) {
[self handleAcceptActionWithNotification:notification];
}
// Must be called when finished
completionHandler();
}
-(void) handleAcceptActionWithNotification:(NSDictionary *)notification {
UIAlertView *test = [[UIAlertView alloc] initWithTitle:@"YAY" message:@"Success" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[test show];
[self.tabBarController setSelectedIndex:1];
[self performSelector:@selector(launchNews) withObject:nil afterDelay:1.0];
}
-(void)launchNews {
NewsViewController *dvController8 = [[NewsViewController alloc] initWithNibName:@"NewsViewController" bundle:[NSBundle mainBundle]];
[self.tabBarController.navigationController pushViewController:dvController8 animated:YES];
[dvController8 release];
}
但是,是否有一种方法可以在收到某个类别的推送通知时执行自定义操作,而无需在通知中添加操作按钮?
这是我的Payload,使用cloudCode:
Parse.Cloud.define("newNews", function(request, response) {
var theTitle = request.params.articleTitle;
var pushQuery = new Parse.Query(Parse.Installation);
Parse.Push.send({
where: pushQuery,
data: {
alert: "A new article, \"" + theTitle + "\" was just posted. Open app to view.",
category : "TAGGED_CATEGORY",
sound: "default.caf"
}
}).then(function() {
response.success("Push was sent successfully.")
}, function(error) {
response.error("Push failed to send with error: " + error.message);
});
});
UPDATED CODE:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
if (application.applicationState == UIApplicationStateInactive) {
[self handleRemoteNotificationWithPayload:userInfo];
}
}
-(void)handleRemoteNotificationWithPayload:(NSDictionary *)payload {
NSString *thePayload = [payload valueForKey:@"category"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Hi" message:thePayload delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alertView show];
if ([thePayload isEqualToString:@"TAGGED_CATEGORY"]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Hi" message:@"Received with category" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alertView show];
[self.tabBarController setSelectedIndex:4];
}
}