【问题标题】:Enable/Disable Apple Push Notification from iPhone app?从 iPhone 应用程序启用/禁用 Apple 推送通知?
【发布时间】:2012-05-16 15:52:28
【问题描述】:

我对@9​​87654321@ 还有一个疑问。那是当应用程序首次启动时,如果用户接受,应用程序会请求 Apple 推送通知权限,他们可以接收通知。如果用户取消,他们将不会收到任何通知。我说清楚了吗??

现在我的疑问是,

  1. 如果用户在几天后再次取消应用程序的推送通知服务(单击Cancel 按钮),如果他们希望收到 Apple 推送通知,则可以再次启用 Apple 推送通知来自应用程序的特定用户。

  2. 如果用户先接受苹果推送通知服务,几天后如果他们不想收到通知,是否可以在我们的应用程序中禁用APNS?我希望你能理解我的疑惑。有人可以澄清这个疑问吗?

  3. 在我们的 iPhone 应用中可以做到以上这些场景吗?

请帮助我。提前致谢。

【问题讨论】:

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


    【解决方案1】:

    您可以使用UIRemoteNotificationType enabledTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; 读取您应用的权限,然后对不同类型执行按位和运算以查看启用了哪些权限。您也可以致电unregisterForRemoteNotifications 禁用通知。您不能做的一件事是打开通知,尽管您可以指导用户。

    【讨论】:

      【解决方案2】:

      很遗憾,您无法通过应用代码为您的应用启用或禁用推送通知。请求许可的对话框只显示一次。 通常,其他应用程序会通过进入设置-> 通知-> 应用程序名称向用户显示启用/禁用推送通知的说明。

      【讨论】:

      • 感谢您的回答阿迪格。是否有可能通过代码找到用户关闭以接收来自我的应用程序的通知(来自我们的 iPhone 设备设置屏幕)。提前致谢。
      • 您可以通过以下方式检查是否启用了任何类型的推送通知:if([UIApplication sharedApplication].enabledRemoteNotificationTypes == UIRemoteNotificationTypeNone)
      【解决方案3】:

      我的要求是使用UISwitch 启用和禁用推送通知。为了从代码中启用推送通知,请在按钮操作中使用它。

      [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeBadge |  UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
      

      为了禁用

      [[UIApplication sharedApplication] unregisterForRemoteNotifications];
      NSLog(@"UnRegistered for pushnotification");
      

      【讨论】:

      • 如上所述,启用只能发生一次。
      【解决方案4】:

      1.从您的应用程序不只是在用户安装后第一次打开您的应用程序时出现..如果他决定允许它,他可以从设备设置中激活。

      2.它可以从应用程序和设置中完成..如果您想从您的应用程序中禁用它,您可以将设备令牌(决定禁用推送通知)发送到您的服务器并将其存储为 ex。作为“无通知列表”,并且在发送有效负载时忽略这些令牌,因此它们将不会收到通知。

      3.我已经回答了。

      祝你好运。

      【讨论】:

      • 感谢您的回答 Malek_jundi。是否有可能通过代码找到用户关闭以接收来自我的应用程序的通知(来自我们的 iPhone 设备设置屏幕)。提前致谢。
      • 我不这么认为..但它很简单,一旦用户关闭通知,您将让 api 函数保持 1 个参数(设备令牌)(由您决定如何在您的应用程序,但 UISwitch 它是最佳选择)您将调用此 api 并为此令牌设置一个标志以不接收通知.. 就是这样。
      【解决方案5】:

      当您第一次授予权限时,您的 iPhone 的设备令牌已注册到 APN 服务器,然后您就可以收到推送通知。稍后您可以从您的设备设置 → 通知 → 您的应用启用/禁用。

      【讨论】:

        【解决方案6】:

        您可以使用此代码在 iOS 9 中提供支持

            if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) {
        
            UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
        
            if (types == UIUserNotificationTypeNone) {
                // Do something
                NSLog(@"");
            }
        } else {
        
            UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        
            if (types == UIRemoteNotificationTypeNone) {
                // Do something
                NSLog(@"");
            }
        }
        

        How to update code using enabledRemoteNotificationTypes because it is "not supported in iOS 8"?

        【讨论】:

          【解决方案7】:

          实际上,可以通过注册和注销推送通知来启用和禁用推送通知。

          启用推送通知:

          if #available(iOS 10.0, *) {
             // For iOS 10.0 +
             let center  = UNUserNotificationCenter.current()
             center.delegate = self
             center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
                  if error == nil{
                     DispatchQueue.main.async(execute: {
                           UIApplication.shared.registerForRemoteNotifications()
                     }) 
                  }
             }
          }else{
              // Below iOS 10.0
          
              let settings = UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil)
              UIApplication.shared.registerUserNotificationSettings(settings)
          
              //or
              //UIApplication.shared.registerForRemoteNotifications()
          }
          

          委托方法

          @available(iOS 10.0, *)
          func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
          
          }
          
          @available(iOS 10.0, *)
          func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
          
          }
          
          
          func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
              // .. Receipt of device token
          }
          
          
          func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
              // handle error
          }
          

          禁用推送通知:

          UIApplication.shared.unregisterForRemoteNotifications()
          

          【讨论】:

          • 这意味着,我可以在我的应用程序中为我的应用程序通知设置一个 ON/OFF 开关,并且我可以随时启用/禁用它.. Apple 是否批准这个?
          • @shaqirsaiyed 但我还没有确认,因为我只参与了企业应用程序。但我肯定会用苹果检查这个。这可能非常有用。
          • 是的,如果可能的话,请检查一下。
          猜你喜欢
          • 2023-04-06
          • 1970-01-01
          • 2012-11-28
          • 2011-07-30
          • 1970-01-01
          • 2013-02-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多