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

标签 objective-c json ios8 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”,并让按钮的标题为“选项 A”和“选项 B”。像这样的事情:

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

此外,如果您有是/否问题,您可以使用"is"和“否”选项为其创建一个类别,并使用此类别标记您的通知。

希望这对您有所帮助。

关于objective-c - 更新远程推送通知上的 UIMutableUserNotificationAction 按钮标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28973077/

相关文章:

objective-c - 未找到 NSScrollView 类型

ios - 使用自动布局绘制自定义 UIView 并将其居中作为背景

JavaScript - 2D 数组/对象

json - 如何将单行 JSON 文件转换为人类可读的多行文件?

swift - 使用 Swift 的 iOS 8 中的 Google Analytics 不起作用

iphone - 哪些类可以用作 NSDictionary 中的键?

objective-c - 为什么 awakeFromInsert 被调用两次?

asp.net web api - 接口(interface)成员的 json 序列化导致神秘错误

ios - AFNetworking 2 空 POST 响应但没有错误

ios - Touch ID 导致应用程序无响应