首页 文章

iOS通知中心注册

提问于
浏览
0

我想制作一款测试本地通知的游戏 . 在iOS 8中,代码涉及以下步骤:

  • 创建一个 UIMutableUserNotificationCategory 对象,该对象是您要实现的自定义操作的类别 .

  • 使用 UIMutableUserNotificationAction 实例最多可以执行4个自定义操作 .

  • 调用 setActions:forContext: 设置自定义操作 .

  • 创建 UIUserNotificationSettings 对象并在其上调用 settingsForTypes:categories: .

  • 通过在共享 UIApplication 实例上调用 registerUserNotificationSettings: 来注册应用程序的用户通知设置 .

我的问题是,对于步骤4中提到的 UIUserNotificationSettings ,您如何注册一个类别的应用程序?可以为其设置的值是:

  • UIUserNotificationTypeAlert (仅显示警报)

  • UIUserNotificationTypeBadge (仅显示徽章)

  • UIUserNotificationTypeSound (只播放声音)

  • UIUserNotificationTypeNone (什么都不做)

听起来应用程序应该能够轻松地显示警报,显示徽章和显示声音 . 但是,在通知触发之前,必须注册所有这三个 . 该参数采用 NSUInteger .

我的问题是,如何使它能够访问所有三个权限?你不能传递 NSArray ,所以应该有另一种我没有看到的方式......

2 回答

  • 1

    以此为例:

    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    
  • 2

    以此为例,说明如何添加多个类别

    UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    

相关问题