【问题标题】:Push Notification ON or OFF Checking in iOS在 iOS 中打开或关闭推送通知
【发布时间】:2013-12-20 21:44:53
【问题描述】:

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

}

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

【问题讨论】:

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


    【解决方案1】:
    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");
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果应用程序曾经注册到registerForRemoteNotification,那么您可以禁用和启用。一旦您禁用并且您将要重新注册它,那么这将启用registerForRemoteNotification,而不会弹出警报。

      Technical Note TN2265: Troubleshooting Push Notifications

      支持推送的应用首次注册推送通知时,iOS 询问用户是否希望接收该应用的通知。一次 用户已对此警报做出响应,除非它不会再次显示 设备已恢复或应用程序已卸载至少一段时间 天。

      如果您想模拟您的应用的首次运行,您可以离开 该应用程序卸载了一天。您可以在没有的情况下实现后者 通过将系统时钟提前一天或 更多,完全关闭设备,然后将设备转回 开。

      更多信息:INFO && Info 2

      编辑:对于checking with alert enable -

      使用

       if (types & UIRemoteNotificationTypeAlert){} 
      

      而不是

      if (types == UIRemoteNotificationTypeNone){}
      

      编辑: 最新更新来自doc for iOS 8 or later,您可以通过以下方式查看:

      - (BOOL)isRegisteredForRemoteNotifications
      

      【讨论】:

      • 是的。但是,在一次registerForRemoteNotification 之后,它无法检测到“推送通知选项”第二次是打开还是关闭。如果我的应用程序正在运行,我如何每次都检测到这一点。那就是问题所在。感谢您的评论。 @Kumar Ki
      • 每当您的应用启动时执行 registerForRemoteNotificationTypes 然后自动启用它
      • 您正在做的是,您只是禁用警报而不是使用 RemoteNotification 禁用。这意味着,该应用已经注册了它。所以 if (types == UIRemoteNotificationTypeNone) 变为 false
      • 是的,我明白了。但我必须检查,对于我的特定应用程序或整个设备,设备“推送通知选项”是打开还是关闭?虽然我的应用程序注册了“推送通知”。
      • 检查这个 UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];可以使用 if (types & UIRemoteNotificationTypeAlert){}
      【解决方案3】:

      这对我有用。希望这有帮助! :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);
         }
      }
      

      【讨论】:

      • 我觉得有些cmets可能比ALERT_WITH_TITLE(@"", kMessageNotificationTurnOnRequire);更容易理解//Push Notifications Are Disabled怎么样
      【解决方案4】:

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

      [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
      

      要检查设置是如何设置的,您可以使用:

      [[UIApplication sharedApplication] currentUserNotificationSettings];
      

      【讨论】:

      • 记录:如果用户禁用通知或按下“不允许”,isRegisteredForRemoteNotifications 下次仍然为真。
      • 如果用户按下不允许,isRegisteredForRemoteNotifications 似乎返回 false。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      相关资源
      最近更新 更多