注册远程和本地通知

时间:2016-02-11 10:57:39

标签: ios objective-c notifications apple-push-notifications

在我的应用中,我想同时支持 - Remote NotificationsLocal NotificationsRemote Notification应该有两个操作,允许用户在通知中心与其进行交互。

目前我有这个代码来注册通知。我的问题是我是否需要单独注册本地通知注册,或者本注册过程中已包含本地通知。

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    UIMutableUserNotificationAction *actionCancel = [[UIMutableUserNotificationAction alloc] init];
    [actionCancel setActivationMode:UIUserNotificationActivationModeBackground];
    [actionCancel setTitle:@"Cancel"];
    [actionCancel setIdentifier:NotificationActionOneIdent];
    [actionCancel setDestructive:NO];
    [actionCancel setAuthenticationRequired:NO];

    UIMutableUserNotificationAction *actionOk = [[UIMutableUserNotificationAction alloc] init];
    [actionOk setActivationMode:UIUserNotificationActivationModeBackground];
    [actionOk setTitle:@"Ok"];
    [actionOk setIdentifier:NotificationActionTwoIdent];
    [actionOk setDestructive:NO];
    [actionOk setAuthenticationRequired:NO];

    UIMutableUserNotificationCategory *actionCategory = [[UIMutableUserNotificationCategory alloc] init];
    [actionCategory setIdentifier:NotificationCategoryIdent];
    [actionCategory setActions:@[actionCancel, actionOk]
                    forContext:UIUserNotificationActionContextDefault];

    NSSet *categories = [NSSet setWithObject:actionCategory];
    UIUserNotificationType types = (UIUserNotificationTypeAlert|
                                    UIUserNotificationTypeSound|
                                    UIUserNotificationTypeBadge);

    UIUserNotificationSettings *settings;
    settings = [UIUserNotificationSettings settingsForTypes:types
                                                 categories:categories];

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}

1 个答案:

答案 0 :(得分:0)

注册用户通知设置时,

包含本地人,因此您的代码很好