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

 昧凉生丶楚_139 发布于 2023-02-12 21:00

如果应用程序正在运行(或从恢复模式打开),我想在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];
    }
}

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

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

}

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

3 个回答
  • 在iOS 8中,您现在可以使用:

    [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
    

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

    [[UIApplication sharedApplication] currentUserNotificationSettings];
    

    2023-02-12 21:07 回答
  • 这对我有用.希望这有帮助!: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);
       }
    }
    

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

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

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

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

    更多信息:INFO && Info 2

    编辑:对于checking with alert enable-

    使用

     if (types & UIRemoteNotificationTypeAlert){} 
    

    代替

    if (types == UIRemoteNotificationTypeNone){}
    

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

    - (BOOL)isRegisteredForRemoteNotifications
    

    2023-02-12 21:11 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有