【问题标题】:iOS push notifications permission prompt - what did the user select?iOS推送通知权限提示——用户选择了什么?
【发布时间】:2015-07-15 07:24:20
【问题描述】:

是否有办法知道用户是否拒绝了推送通知权限?

我知道如果用户允许推送通知,将调用 didRegisterForRemoteNotificationsWithDeviceToken - 但如果他不允许,会调用什么?

【问题讨论】:

    标签: ios push-notification apple-push-notifications


    【解决方案1】:

    一个简单的方法来检查应用中是否启用了通知。

    -(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
    
            }
    
        }
    

    【讨论】:

      【解决方案2】:

      根据UIApplicationDelegate Protocol Reference,如果注册过程中出现错误,则调用application(_:didFailToRegisterForRemoteNotificationsWithError:)

      调用UIApplication对象的registerForRemoteNotifications方法后,应用在注册过程中出现错误时调用该方法。

      有关如何在您的应用中实现远程通知的更多信息,请参阅本地和远程通知编程指南。

      【讨论】:

        【解决方案3】:

        您可以通过以下方式实现。假设如果用户允许通知,那么您可以将设备令牌存储到用户默认值中。现在,下次您可以检查是否允许使用相同的用户默认天气用户。

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

        【讨论】:

          猜你喜欢
          • 2023-04-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-23
          相关资源
          最近更新 更多