本文介绍了交互式推送通知-隐藏/显示按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了 本教程 在推送通知上显示按钮.
通过调用didFinishLaunchingWithOptions中的 registerNotification 来注册按钮.
但是,在少数情况下,我需要显示一个没有任何按钮的简单通知.如何显示/隐藏用于不同通知的按钮?

I followed this tutorial to display buttons on a push notification.
Buttons are registered by calling registerNotification in didFinishLaunchingWithOptions.
However, in few cases I need to display a simple notification without any buttons. How to show/hide the buttons for different notifications ?

推荐答案

要添加另一种没有按钮的交互式通知,您将必须更新UIUserNotificationSettings.

For adding another kind of interactive notification with no button you will have to update the UIUserNotificationSettings.

创建没有任何按钮的新通知类别UIMutableUserNotificationCategory:

Create a new notification category UIMutableUserNotificationCategory without any button:

UIMutableUserNotificationCategory *newNotificationCategory = [[UIMutableUserNotificationCategory alloc] init];
newNotificationCategory.identifier = @"no_button_id";

然后,将此新类别添加到现有的UIUserNotificationSettings:

Then, add this new category to the existing UIUserNotificationSettings:

    NSMutableArray *arrNewCategories = [NSMutableArray new];
    UIUserNotificationSettings *oldSettings = [[UIApplication sharedApplication]currentUserNotificationSettings];
    for (UIMutableUserNotificationCategory *oldCategory in oldSettings.categories)
    {
      if (![oldCategory.identifier isEqualToString:newNotificationCategory.identifier])
           [arrNewCategories addObject:oldCategory];
    }
    [arrNewCategories addObject:newNotificationCategory];

    UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    UIUserNotificationSettings *newSettings = [UIUserNotificationSettings settingsForTypes:notificationType categories:[NSSet setWithArray:arrNewCategories]];

    [[UIApplication sharedApplication] registerUserNotificationSettings:newSettings];

只需确保newNotificationCategory的标识符与UILocalNotification的类别匹配即可,在该类别中您不需要任何按钮.

Just make sure that the identifier of newNotificationCategory matches with your UILocalNotification's category, in which you dont need any buttons.

然后安排通知:

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:afireDate];
localNotification.alertBody = @"alert body text";
localNotification.category = @"no_button_id"; //  Same as category identifier
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.soundName = SOUND_FILE;
localNotification.repeatInterval = 0;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];

这篇关于交互式推送通知-隐藏/显示按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 03:56