iOS 12 临时通知 UNAuthorizationOptionProvisional

353 阅读2分钟

在 iOS 中,临时通知(Provisional Notification)允许应用在用户未明确授予通知权限的情况下向其发送静默通知。这通常用于提供非侵入性的通知体验,并让用户决定是否允许更持久的通知权限。

启用临时通知的方法

要在 Objective-C 中启用临时通知,可以按照以下步骤进行:

  1. 请求临时通知权限

临时通知权限的关键是使用 UNAuthorizationOptionProvisional 选项。

#import <UserNotifications/UserNotifications.h>

- (void)requestProvisionalNotificationAuthorization {
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    UNAuthorizationOptions options = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge | UNAuthorizationOptionProvisional;

    [center requestAuthorizationWithOptions:options
                          completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            NSLog(@"Provisional notification authorization granted.");
        } else {
            NSLog(@"Provisional notification authorization denied: %@", error);
        }
    }];
}
  1. 配置通知内容并发送本地通知

当权限被授予后,可以发送本地通知:

- (void)scheduleLocalNotification {
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    
    // 创建通知内容
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    content.title = @"临时通知标题";
    content.body = @"这是临时通知的内容";
    content.sound = [UNNotificationSound defaultSound];
    
    // 设置触发时间 (例如,5秒后触发)
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
    
    // 创建通知请求
    NSString *identifier = @"ProvisionalNotification";
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
    
    // 添加通知请求
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"Failed to add provisional notification: %@", error);
        } else {
            NSLog(@"Provisional notification scheduled successfully.");
        }
    }];
}
  1. 检查通知授权状态

可以在需要时检查当前的授权状态:

- (void)checkNotificationAuthorizationStatus {
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        switch (settings.authorizationStatus) {
            case UNAuthorizationStatusNotDetermined:
                NSLog(@"Authorization status: Not determined");
                break;
            case UNAuthorizationStatusDenied:
                NSLog(@"Authorization status: Denied");
                break;
            case UNAuthorizationStatusAuthorized:
                NSLog(@"Authorization status: Authorized");
                break;
            case UNAuthorizationStatusProvisional:
                NSLog(@"Authorization status: Provisional");
                break;
            default:
                break;
        }
    }];
}

注意事项

1.	支持的 iOS 版本:临时通知功能从 iOS 12 开始支持。
2.	用户体验:临时通知不会立即弹出权限请求窗口,而是以非侵入式的方式通知用户,通知会出现在通知中心。
3.	权限升级:当用户从通知中心交互后,系统可能会提示他们为应用启用完整的通知权限。

通过这种方式,可以有效减少通知权限的拒绝率,同时提供更好的用户体验。

自定义通知横幅弹窗可参考 github.com/cruffenach/…