更新远程推送通知上的 UIMutableUserNotificationAction 按钮标题
Posted
技术标签:
【中文标题】更新远程推送通知上的 UIMutableUserNotificationAction 按钮标题【英文标题】:Update UIMutableUserNotificationAction Button Title on Remote Push Notification 【发布时间】:2015-03-10 20:03:26 【问题描述】:我在 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"
【问题讨论】:
【参考方案1】:很抱歉,您不能动态更改按钮的标题。它们与您注册通知时所存储的类别相关联。
一种可能的解决方法是在消息文本中显示两个选项,分别标有“A”和“B”,并让您的按钮标题为“选项 A”和“选项 B”。像这样的:
---------------------------------
|Who's your favorite pop singer?|
|A: Katy Perry B: Miley Cyrus|
---------------------------------
| Option A | Option B |
---------------------------------
此外,如果您有是/否问题,您可以使用“是”和“否”选项为他们创建一个类别,并使用此类别标记您的通知。
我希望这会有所帮助。
【讨论】:
这确实有帮助!谢谢你,好主意! 只有当应用程序在前台时,我才能更新按钮标题。以上是关于更新远程推送通知上的 UIMutableUserNotificationAction 按钮标题的主要内容,如果未能解决你的问题,请参考以下文章