在 registerUserNotificationSettings 之后立即调用 registerForRemoteNotifications?
calling registerForRemoteNotifications immediately after registerUserNotificationSettings?
根据 apple 的 guide,它建议以这种方式注册通知:
- (void)applicationDidFinishLaunching:(UIApplication *)app {
// other setup tasks here....
// Register the supported interaction types.
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
// Register for remote notifications.
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
如上图,在registerUserNotificationSettings
后立即调用registerForRemoteNotifications
,但是第一次打开应用时,获取不到成功访问令牌,因为用户尚未授予通知权限。
这样,应用程序第二次打开时,将获得访问令牌。
苹果为什么这么建议?
我建议在didRegisterUserNotificationSettings
中调用registerForRemoteNotifications
,因为它会在用户授予通知权限后调用。
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings){
application.registerForRemoteNotifications()
}
我的建议正确吗?
您的建议是正确的。你可能总是想要 post 一个 NSNotification 是你想在你的视图控制器中处理失败。
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
if (notificationSettings.types != UIUserNotificationTypeNone) {
//register to receive notifications
[application registerForRemoteNotifications];
} else {
// same as response to didFailToRegisterForRemoteNotificationsWithError
NSDictionary* data = [NSDictionary dictionaryWithObject:@"" forKey:@"deviceToken"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"notificationsRegistered" object:self userInfo:data];
}
}
您还应该检查通知错误的条件。
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
print("Got token data! \(deviceToken)")
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Couldn't register: \(error)")
}
如果您想检查通知设置,请使用此方法:
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings){
}
根据 apple 的 guide,它建议以这种方式注册通知:
- (void)applicationDidFinishLaunching:(UIApplication *)app {
// other setup tasks here....
// Register the supported interaction types.
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
// Register for remote notifications.
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
如上图,在registerUserNotificationSettings
后立即调用registerForRemoteNotifications
,但是第一次打开应用时,获取不到成功访问令牌,因为用户尚未授予通知权限。
这样,应用程序第二次打开时,将获得访问令牌。
苹果为什么这么建议?
我建议在didRegisterUserNotificationSettings
中调用registerForRemoteNotifications
,因为它会在用户授予通知权限后调用。
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings){
application.registerForRemoteNotifications()
}
我的建议正确吗?
您的建议是正确的。你可能总是想要 post 一个 NSNotification 是你想在你的视图控制器中处理失败。
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
if (notificationSettings.types != UIUserNotificationTypeNone) {
//register to receive notifications
[application registerForRemoteNotifications];
} else {
// same as response to didFailToRegisterForRemoteNotificationsWithError
NSDictionary* data = [NSDictionary dictionaryWithObject:@"" forKey:@"deviceToken"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"notificationsRegistered" object:self userInfo:data];
}
}
您还应该检查通知错误的条件。
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
print("Got token data! \(deviceToken)")
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Couldn't register: \(error)")
}
如果您想检查通知设置,请使用此方法:
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings){
}