推送通知ON或OFF在iOS中检查

Tul*_*lon 21 iphone push-notification apple-push-notifications ipad ios

如果应用程序正在运行(或从恢复模式打开),我想在iOS设备中检查"推送通知选项".如果选项为OFF,我使用以下代码进行检查:

-(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];
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我使用以下代码进入"设置选项卡>>通知中心",以便用户可以手动操作:

- (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)];
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

但是,现在我遇到的问题是,它只在启动应用程序后第一次出现.它按我的意愿工作.但在那之后,如果我从"设置"关闭"推送通知选项",它不会给我"警报消息".

thi*_*sai 41

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

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
Run Code Online (Sandbox Code Playgroud)

要检查设置的设置方式,您可以使用:

[[UIApplication sharedApplication] currentUserNotificationSettings];
Run Code Online (Sandbox Code Playgroud)

  • 对于记录:如果用户禁用通知或按下"不允许",则"isRegisteredForRemoteNotifications"下次仍然为真. (4认同)

Kum*_* KL 21

如果应用程序曾经注册过registerForRemoteNotification,那么您可以禁用以及启用.一旦你禁用并且即将使用它重新注册,那么这将启用 registerForRemoteNotification,而不使用Popup进行警报.

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

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

如果您想模拟首次运行的应用,可以将应用程序卸载一天.通过将系统时钟向前设置一天或更长时间,完全关闭设备,然后重新打开设备,您可以实现后者而无需实际等待一天.

更多信息:INFO && Info 2

编辑:对于checking with alert enable-

使用

 if (types & UIRemoteNotificationTypeAlert){} 
Run Code Online (Sandbox Code Playgroud)

代替

if (types == UIRemoteNotificationTypeNone){}
Run Code Online (Sandbox Code Playgroud)

编辑:iOS 8或更高版本doc的最新更新,您可以通过以下方式查看:

- (BOOL)isRegisteredForRemoteNotifications
Run Code Online (Sandbox Code Playgroud)


小智 8

这对我有用.希望这有帮助!: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);
   }
}
Run Code Online (Sandbox Code Playgroud)