交互式推送通知 - 隐藏/显示按钮 [英] Interactive push notifications - Hide/Show buttons

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

问题描述

我遵循了本教程在推送通知上显示按钮.
通过在 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];

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆