更新远程推送通知上的 UIMutableUserNotificationAction 按钮标题

Update UIMutableUserNotificationAction Button Title on Remote Push Notification

我在 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"
 }

对不起,您不能动态更改按钮的标题。当您注册通知时,它们与存储它们的类别相关联。

一个可能的解决方法是在标有 'A' 和 'B' 的消息文本中显示这两个选项,并让您的按钮标题为 'Option A' 和 'Option B'。像这样:

---------------------------------
|Who's your favorite pop singer?|
|A: Katy Perry    B: Miley Cyrus|
---------------------------------
|   Option A    |   Option B    |
---------------------------------

此外,如果您有 yes/no 个问题,您可以使用选项 'Yes' 和 'No' 为它们创建一个类别,并使用此类别标记您的通知。

希望对您有所帮助。