在iOS中推送通知开启或closures检查

我想检查iOS设备中的“推送通知选项”,只要应用程序正在运行(或从恢复模式开启)。 我使用下面的代码来检查,如果选项是closures的:

-(void)PushNotificationServiceChecking { UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; if (types == UIRemoteNotificationTypeNone) { NSString *msg = @"Please press ON to enable Push Notification"; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Push Notification Service Disable" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"ON", nil]; alert.tag = 2; [alert show]; } } 

然后,我使用下面的代码去“设置”选项卡>>通知中心,以便用户可以手动:

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (alertView.tag == 2) { if (buttonIndex == 0) { // this is the cancel button } else if (buttonIndex == 1) { [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert)]; } } } 

但是,现在我面临的问题是,它只是在启动应用程序后第一次出现。 它按我的意思工作。 但之后,如果我从“设置”中closures“推送通知选项”,则不会给出“提示消息”。

如果应用程序曾经注册registerForRemoteNotification ,那么你可以禁用和启用。 一旦你禁用,你将重新configuration它,那么这将启用registerForRemoteNotification ,没有popup警报。

技术说明TN2265:排除推送通知

当推送应用程序第一次注册推送通知时,iOS会询问用户是否希望接收该应用程序的通知。 一旦用户对此警报作出响应,除非设备已恢复或应用程序已卸载至less一天,否则不会再次显示。

如果您想模拟首次运行您的应用,则可以将该应用卸载一天。 您可以实现后者,而无需等待一天,方法是将系统时钟设置为一天或更长时间,完全closures设备,然后重新打开设备。

前景更多信息: 信息 && 信息2

编辑 :用于checking with alert enable

使用

  if (types & UIRemoteNotificationTypeAlert){} 

代替

 if (types == UIRemoteNotificationTypeNone){} 

编辑:最新的文档更新为iOS 8或更高版本 ,您可以检查出:

 - (BOOL)isRegisteredForRemoteNotifications 

在iOS 8中,您现在可以使用:

 [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]; 

并检查如何设置设置您可以使用:

 [[UIApplication sharedApplication] currentUserNotificationSettings]; 

这对我有用。 希望这个帮助! :d

 #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")){ UIUserNotificationType type = [[[UIApplication sharedApplication] currentUserNotificationSettings] types]; if (type == UIUserNotificationTypeNone){ ALERT_WITH_TITLE(@"", kMessageNotificationTurnOnRequire); } } else { UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; if (types == UIRemoteNotificationTypeNone) { ALERT_WITH_TITLE(@"", kMessageNotificationTurnOnRequire); } } 
  NSString *iOSversion = [[UIDevice currentDevice] systemVersion]; NSString *prefix = [[iOSversion componentsSeparatedByString:@"."] firstObject]; float versionVal = [prefix floatValue]; if (versionVal >= 8) { if ([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone) { NSLog(@" Push Notification ON"); } else { NSString *msg = @"Please press ON to enable Push Notification"; UIAlertView *alert_push = [[UIAlertView alloc] initWithTitle:@"Push Notification Service Disable" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Setting", nil]; alert_push.tag = 2; [alert_push show]; NSLog(@" Push Notification OFF"); } } else { UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; if (types != UIRemoteNotificationTypeNone) { NSLog(@" Push Notification ON"); } else { NSString *msg = @"Please press ON to enable Push Notification"; UIAlertView *alert_push = [[UIAlertView alloc] initWithTitle:@"Push Notification Service Disable" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Setting", nil]; alert_push.tag = 2; [alert_push show]; NSLog(@" Push Notification OFF"); } }