iOS推送通知权限提示-用户select做了什么?
iOS push notifications permission prompt - what did the user select?
这是一种了解用户是否拒绝推送通知权限的方法吗?
我知道 didRegisterForRemoteNotificationsWithDeviceToken 将在用户允许推送通知的情况下被调用 - 但如果他不允许,将调用什么?
您可以通过以下方式实现。假设如果用户允许通知,那么您可以将设备令牌存储到用户默认值中。现在,下次您可以检查是否允许使用同一用户的默认天气用户。
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSString *aStrToken = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
aStrToken = [aStrToken stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *aStrStoredToken = [_appDelegate validateString:[[NSUserDefaults standardUserDefaults]objectForKey:@"deviceID"] :@""];
if([aStrStoredToken length]==0) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"getDeviceToken" object:nil];
[self setApplicationBadgeNumber:0];
[[NSUserDefaults standardUserDefaults]setObject:aStrToken forKey:@"deviceID"];
[[NSUserDefaults standardUserDefaults]synchronize];
if(![[NSUserDefaults standardUserDefaults]objectForKey:@"setDeviceID"]) {
[[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"setDeviceID"];
[[NSUserDefaults standardUserDefaults]synchronize];
} else {
[[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"setDeviceID"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
}
}
根据UIApplicationDelegate Protocol Reference,如果在注册过程中发生错误,将调用application(_:didFailToRegisterForRemoteNotificationsWithError:)
。
After you call the registerForRemoteNotifications method of the UIApplication object, the app calls this method when there is an error in the registration process.
有关如何在您的应用中实现远程通知的详细信息,请参阅本地和远程通知编程指南。
一个简单的方法来检查是否在应用程序中启用了通知。
-(BOOL)checkNotificationEnabled
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone)
{
return FALSE; //Notification not enabled
}
else
{
return TRUE;//Notification is enabled
}
}
else // for iOS 8 devices checking will be different
{
return [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
// if return TRUE then Notification is enabled
// if return False then Notification is not enabled
}
}
这是一种了解用户是否拒绝推送通知权限的方法吗?
我知道 didRegisterForRemoteNotificationsWithDeviceToken 将在用户允许推送通知的情况下被调用 - 但如果他不允许,将调用什么?
您可以通过以下方式实现。假设如果用户允许通知,那么您可以将设备令牌存储到用户默认值中。现在,下次您可以检查是否允许使用同一用户的默认天气用户。
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSString *aStrToken = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
aStrToken = [aStrToken stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *aStrStoredToken = [_appDelegate validateString:[[NSUserDefaults standardUserDefaults]objectForKey:@"deviceID"] :@""];
if([aStrStoredToken length]==0) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"getDeviceToken" object:nil];
[self setApplicationBadgeNumber:0];
[[NSUserDefaults standardUserDefaults]setObject:aStrToken forKey:@"deviceID"];
[[NSUserDefaults standardUserDefaults]synchronize];
if(![[NSUserDefaults standardUserDefaults]objectForKey:@"setDeviceID"]) {
[[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"setDeviceID"];
[[NSUserDefaults standardUserDefaults]synchronize];
} else {
[[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"setDeviceID"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
}
}
根据UIApplicationDelegate Protocol Reference,如果在注册过程中发生错误,将调用application(_:didFailToRegisterForRemoteNotificationsWithError:)
。
After you call the registerForRemoteNotifications method of the UIApplication object, the app calls this method when there is an error in the registration process.
有关如何在您的应用中实现远程通知的详细信息,请参阅本地和远程通知编程指南。
一个简单的方法来检查是否在应用程序中启用了通知。
-(BOOL)checkNotificationEnabled
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone)
{
return FALSE; //Notification not enabled
}
else
{
return TRUE;//Notification is enabled
}
}
else // for iOS 8 devices checking will be different
{
return [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
// if return TRUE then Notification is enabled
// if return False then Notification is not enabled
}
}