我在iOS上有一个应用程序,我向用户发送快速调查。我想利用iOS 8中的新功能,用户可以直接在推送通知上进行交互。用户将收到调查问题并作出回应。
我知道您必须在启动应用时构建您的操作和类别。我编写了我的代码,并且迄今为止已成功执行此操作。
现在,问题是操作按钮上的文字会根据调查问题而改变。我在我发送的JSON上包含操作按钮的键和文本,但是在收到通知时我无法更改操作按钮标题。
有办法做到这一点吗?这是我的代码:
我定义全局变量:
//This will help configure the interactive notifications, so that the user can reply to push notification from out of the app
NSString * const notificationCategoryIdent = @"SURVEY";
NSString * const notificationActionPositiveButton = @"POSITIVE_BUTTON";
NSString * const notificationActionNegativeButton = @"NEGATIVE_BUTTON";
在我的AppDelegate中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if(pushNotificationPayload) {
urlPushData2 = @"Application was closed";
[self application:[UIApplication sharedApplication] didReceiveRemoteNotification:pushNotificationPayload];
}
//Registrar tipos de aviso para Push Notifications
//if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]){
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0){ //Before iOS 8
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)];
}
else {
// define notification actions (In case of categorized remote push notifications)
positiveButton = [[UIMutableUserNotificationAction alloc] init];
[positiveButton setActivationMode:UIUserNotificationActivationModeForeground];
[positiveButton setTitle:notificationActionPositiveButton];
[positiveButton setIdentifier:notificationActionPositiveButton];
[positiveButton setDestructive:NO];
// this action wil be executed without necessity of passcode authentication (if locked)
[positiveButton setAuthenticationRequired:NO];
negativeButton = [[UIMutableUserNotificationAction alloc] init];
[negativeButton setActivationMode:UIUserNotificationActivationModeForeground];
[negativeButton setTitle:notificationActionNegativeButton];
[negativeButton setIdentifier:notificationActionNegativeButton];
[negativeButton setDestructive:NO];
// this action wil be executed without necessity of passcode authentication (if locked)
[negativeButton setAuthenticationRequired:NO];
// define Categories (In case of categorized remote push notifications)
UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:notificationCategoryIdent];
[actionCategory setActions:@[positiveButton, negativeButton]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObject:actionCategory];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types
categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
//Boolean value for temporary app interruptions
temporaryInactivity = FALSE;
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"remote notification: %@",[userInfo description]);
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
NSString *alert = [apsInfo objectForKey:@"alert"];
//NSLog(@"Received Push Alert: %@", alert);
NSString *sound = [apsInfo objectForKey:@"sound"];
NSLog(@"Received Push Sound: %@", sound);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];
self.mainUrl = [userInfo valueForKey:@"url"];
urlPushData = [NSString stringWithFormat:@"%@", mainUrl];
//NSLog(@"urlPushData: %@",urlPushData);
self.mainUrl2 = [userInfo valueForKey:@"url2"];
urlPushData2 = [NSString stringWithFormat:@"%@", mainUrl2];
//NSLog(@"urlPushData2: %@",urlPushData2);
NSString *category = [apsInfo objectForKey:@"category"];
NSLog(@"category: %@", category);
if ([category isEqualToString:notificationCategoryIdent]) {
NSLog(@"button1: %@", [apsInfo objectForKey:@"positiveButton"]);
NSLog(@"button2: %@", [apsInfo objectForKey:@"negativeButton"]);
NSString *stringPositiveButton = [apsInfo objectForKey:@"positiveButton"];
NSString *stringNegativeButton = [apsInfo objectForKey:@"negativeButton"];
[positiveButton setTitle:stringPositiveButton];
[negativeButton setTitle:stringNegativeButton];
}
else {
if (![urlPushData2 isEqualToString:@"Application was closed"]) {
[UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber - 1;
[[NSUserDefaults standardUserDefaults] setObject:urlPushData2 forKey:@"pushNotificationURL"];
[[NSUserDefaults standardUserDefaults] setObject:alert forKey:@"pushNotificationURLName"];
[[NSUserDefaults standardUserDefaults]synchronize];
if (NSClassFromString(@"UIAlertController")) {
//make and use a UIAlertController
UIAlertController *alertView = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%@",appNameLink]
message:[NSString stringWithFormat:@"Has recibido una nueva nota: %@. ¿Deseas verla?",alert]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *Cancel = [UIAlertAction actionWithTitle:@"No"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber - 1;
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"pushNotificationURL"];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"pushNotificationURLName"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
];
UIAlertAction *OK = [UIAlertAction actionWithTitle:@"Sí"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber - 1;
self.url = [[NSUserDefaults standardUserDefaults] objectForKey:@"pushNotificationURL"];
self.urlName = [[NSUserDefaults standardUserDefaults] objectForKey:@"pushNotificationURLName"];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"pushNotificationURL"];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"pushNotificationURLName"];
[[NSUserDefaults standardUserDefaults]synchronize];
[self setupNavigationControllerApp];
}
];
[alertView addAction:Cancel];
[alertView addAction:OK];
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:alertView animated:YES completion:nil];
}
else {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"%@",appNameLink] message:[NSString stringWithFormat:@"Has recibido una nueva nota: %@. ¿Deseas verla?",alert] delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Sí", nil];
[alertView show];
}
} else {
[UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber - 1;
//self.url = [[NSUserDefaults standardUserDefaults]objectForKey:@"pushNotificationURL"];
self.url = urlPushData2;
self.urlName = alert;
[self setupNavigationControllerApp];
}
}
}
- (void) application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {
if ([identifier isEqualToString:notificationActionPositiveButton]) {
//Testing the action button
self.url = @"http://www.google.com";
self.urlName = @"hello";
[self setupNavigationControllerApp];
}
else if ([identifier isEqualToString:notificationActionNegativeButton]) {
//Testing the action button
self.url = @"http://www.yahoo.com";
self.urlName = @"goodbye";
[self setupNavigationControllerApp];
}
// Must be called when finished
completionHandler();
}
这是我的JSON:
{"aps":
{"alert":"Who's your favorite pop singer?",
"badge":1,
"sound":"default",
"category":"SURVEY",
"positiveButton":"Katy Perry",
"negativeButton":"Miley Cyrus"
},
"url":"http://www.pretechmobile.com",
"url2":"http://goo.gl/U9p1y"
}
答案 0 :(得分:3)
很抱歉,您无法动态更改按钮的标题。当您注册通知时,它们与存储它们的类别相关联。
可能的解决方法可能是在标有“A&#39; A&#39;”的消息文本中显示这两个选项。和一个&#39; B&#39;并使用标题按钮&#39;选项A&#39;和&#39;选项B&#39;像这样:
for (int i = 0; i < rows.size(); i++) {
Bean line = new Bean();
ArrayList al=(ArrayList)rows.get(i);//now [FRPP, PE103, , USD]
line.setField1((String)al.get(0));//FRPP
line.setField2((String)al.get(1));//PE103
line.setField3((String)al.get(2));//
line.setField4((String)al.get(3));//USD
data.add(line);
}
此外,如果您有/无问题,可以使用选项&#39;是&#39;为他们创建一个类别。和&#39;不&#39;并使用此类别标记您的通知。
我希望这有点帮助。