始终启用iOS 8+远程通知功能 [英] iOS 8+ Remote notification capability always enabled
问题描述
对于iOS> = 8,只有
For iOS >= 8, only
在我的AppDelegate中,我按如下方式注册用户通知:
In my AppDelegate, I register for user notifications as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"didFinishLaunchingWithOptions called");
// iOS >= 8.0
// register to receive user notifications
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
...
}
完成后,我注册远程通知如下:
Upon completion, I register for remote notification as follows:
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
NSLog(@"didRegisterUserNotificationSettings called");
//register to receive remote notifications
[application registerForRemoteNotifications];
}
完成后,我会检查该应用是否已注册
And upon completion, I check to see whether the app is registered
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken called");
BOOL pushNotificationOnOrOff = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]; // always returns TRUE
}
但是app总是表示推送通知已启用,甚至当用户明确设置应用程序的远程通知功能时(通过首次安装应用程序后显示的通知权限提醒,或通过应用程序设置)。
But app always indicates that push notification is enabled, even when the user has explicitly set the app's remote notification capability off (via the notification permissions alert that appears after first time installation of the app, or via app settings.)
I已将应用程序的后台模式/远程通知设置为TRUE。我一直在使用开发证书编译的实际设备上进行调试(通过USB线连接)。
I have set the app's Background Modes / Remote Notifications to TRUE. I have been debugging on an actual device (tethered via USB cable) compiled with a development certificate.
帮助,我一直在争夺这个数小时。
Help, I've been battling this for hours.
推荐答案
这似乎是一个错误,我也在iPhone 6,iOS 8.1.2上发现了相同的行为。
This seems to be a bug, I also discovered the same behaviour on iPhone 6, iOS 8.1.2.
[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]
总是返回 TRUE
即使用户拒绝推送通知权限(通过提醒视图)或通过 Settings.app>手动禁用通知通知
。
[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]
always returns TRUE
even if user declined the push notification permission (via the alert view) or by manually disabling the notification via Settings.app > Notifications
.
经过一些研究后,我发现如果您已启用后台应用刷新
对于应用程序,然后 [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]
将始终返回 TRUE
。
After some research I discovered that if you have Background App Refresh
enabled for the app, then [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]
will always returns TRUE
.
当后台应用刷新
设置为 FALSE
然后 [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]
返回正确的值。
When Background App Refresh
set to FALSE
then [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]
returns the correct value.
作为解决方法,您可以评估 [[UIApplication sharedApplication] currentUserNotificationSettings] .types
确定是否允许推送通知。
As a workaround, you can evaluate the [[UIApplication sharedApplication] currentUserNotificationSettings].types
to determine whether the push notification is allowed or not.
typedef NS_OPTIONS(NSUInteger, UIUserNotificationType) {
UIUserNotificationTypeNone = 0, // the application may not present any UI upon a notification being received
UIUserNotificationTypeBadge = 1 << 0, // the application may badge its icon upon a notification being received
UIUserNotificationTypeSound = 1 << 1, // the application may play a sound upon a notification being received
UIUserNotificationTypeAlert = 1 << 2, // the application may display an alert upon a notification being received
} NS_ENUM_AVAILABLE_IOS(8_0);
希望这会有所帮助。
这篇关于始终启用iOS 8+远程通知功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!