Добавьте действие UNNotificationAction в конкретное уведомление - PullRequest
0 голосов
/ 30 мая 2018

Я хочу добавить UNNotificationAction в свое приложение для push-уведомлений, которое я сделал с помощью следующего кода: -

[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
        UNNotificationAction *accept = [UNNotificationAction actionWithIdentifier:kAcceptIdentifier
                                                                        title:NSLocalizedString(@"Accept", nil)
                                                                      options:UNNotificationActionOptionForeground];
        UNNotificationAction *reject = [UNNotificationAction actionWithIdentifier:kRejectIdentifier
                                                                            title:NSLocalizedString(@"Reject", nil)
                                                                          options:UNNotificationActionOptionForeground];
        NSArray *buttons = @[ accept, reject ];

        // create a category for message failed
        UNNotificationCategory *buttonsAction = [UNNotificationCategory categoryWithIdentifier:@"Chat_Request"
                                                                                       actions:buttons
                                                                             intentIdentifiers:@[]
                                                                                options:UNNotificationCategoryOptionCustomDismissAction];
        NSSet *categories = [NSSet setWithObjects:buttonsAction, nil];

        // registration
        [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categories];
    }];

Но теперь я хочу, чтобы эти действия появлялись в некоторых определенных push-уведомлениях, полученных от сервера.В настоящее время он отображается во всех уведомлениях.Я не уверен, как это сделать.

Заранее спасибо.

1 Ответ

0 голосов
/ 30 мая 2018

Я решил это сам с помощью следующего кода:

[UNUserNotificationCenter currentNotificationCenter].delegate = self;
    UNAuthorizationOptions authOptions =
    UNAuthorizationOptionAlert
    | UNAuthorizationOptionSound
    | UNAuthorizationOptionBadge;
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
        UNNotificationAction *accept = [UNNotificationAction actionWithIdentifier:kAcceptIdentifier
                                                                        title:NSLocalizedString(@"Accept", nil)
                                                                      options:UNNotificationActionOptionForeground];
        UNNotificationAction *reject = [UNNotificationAction actionWithIdentifier:kRejectIdentifier
                                                                            title:NSLocalizedString(@"Reject", nil)
                                                                          options:UNNotificationActionOptionForeground];
        NSArray *buttons = @[ accept, reject ];

        // create a category for action
        UNNotificationCategory *buttonsAction = [UNNotificationCategory categoryWithIdentifier:@"Chat_Request"
                                                                                       actions:buttons
                                                                             intentIdentifiers:@[]
                                                                                options:UNNotificationCategoryOptionCustomDismissAction];
        NSSet *categories = [NSSet setWithObjects:buttonsAction, nil];

        // registration
        [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categories];
    }];

Убедитесь, что ваша полезная нагрузка должна содержать ключ «категория» с тем же идентификатором, что и ваш код в вашем коде.Например:

{"aps":{"alert":"Testing2","badge":1,"sound":"default","category":"Chat_Request"}}
...