这个问题与iOS 10的APNS更改相关。
这是应用程序的流程:
- 安装应用
- 应用启动 ➝ 登录界面
- 成功登录 ➝ 主屏幕
- 推送通知 ➝ 请求
- 推送通知 ➝ 不允许
- 关闭应用
- 设置 ➝ 用户启用推送通知
- 打开应用
- 如何检查设置是否更新?
- 关闭应用
- 设置 ➝ 用户禁用推送通知
- 打开应用
- 如何检查设置是否更新?
只有当用户登录时,我才会请求推送通知(步骤4)。因此,在用户注销之前,我将无法重新请求推送通知。
是否有一种简洁明了的解决方案,可以支持iOS 10的更改,并同时支持iOS 8或9?
这个问题与iOS 10的APNS更改相关。
这是应用程序的流程:
只有当用户登录时,我才会请求推送通知(步骤4)。因此,在用户注销之前,我将无法重新请求推送通知。
是否有一种简洁明了的解决方案,可以支持iOS 10的更改,并同时支持iOS 8或9?
在iOS8中,UIUserNotificationSettings已被弃用。如果您想访问应用程序设置的一般状态,请查看UNUserNotifications,这是一个新框架。我的理解是,它将推送和本地通知视为同一事物。当您注册通知时,可以调用注册推送。但是对于本地权限 - 如标记等,仍需要请求用户权限。也就是说,您的设备可以接受推送通知而无需用户许可以接收数据更新,但是您只能通过权限来显示通知。以下是查看已授予的权限的方法。
Import the framework into your class
@import UserNotifications;
Query the settings
- (void)_queryNotificationsStatus
{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings){
//1. Query the authorization status of the UNNotificationSettings object
switch (settings.authorizationStatus) {
case UNAuthorizationStatusAuthorized:
NSLog(@"Status Authorized");
break;
case UNAuthorizationStatusDenied:
NSLog(@"Status Denied");
break;
case UNAuthorizationStatusNotDetermined:
NSLog(@"Undetermined");
break;
default:
break;
}
//2. To learn the status of specific settings, query them directly
NSLog(@"Checking Badge settings");
if (settings.badgeSetting == UNAuthorizationStatusAuthorized)
NSLog(@"Yeah. We can badge this puppy!");
else
NSLog(@"Not authorized");
}];
}
if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) {
// yes
}else{
// no
}
当你的应用程序进入前台时,可以使用 getNotificationSettingsWithCompletionHandler。
-(void) IsNotifictaionEnabled :(void (^)(BOOL isActive))handler {
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
if (settings.alertSetting == UNNotificationSettingEnabled) {
handler(YES);
} else {
handler(NO);
}
}];
}
以下是原始答案,但现在currentUserNotificationSettings已经被弃用。
当您的应用程序进入前台时,您可以使用currentUserNotificationSettings。
UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (grantedSettings.types == UIUserNotificationTypeNone) {
NSLog(@"No permiossion granted");
}
else if (grantedSettings.types & UIUserNotificationTypeSound & UIUserNotificationTypeAlert ){
NSLog(@"Sound and alert permissions ");
}
else if (grantedSettings.types & UIUserNotificationTypeAlert){
NSLog(@"Alert Permission Granted");
}
如果您想检查状态是否与先前的状态发生了变化,可以将currentUserNotificationSettings
的先前值保存到某个变量中,并在applicationWillEnterForeground
方法中随时间比较其当前值。