【问题标题】:Enable or Disable Iphone Push Notifications inside the app在应用程序内启用或禁用 iPhone 推送通知
【发布时间】:2023-04-06 03:36:01
【问题描述】:

我有一个可以接收推送通知的 iphone 应用程序。目前,我可以通过转到 iphone 设置/通知来禁用我的应用程序的推送通知。

但我想在我的应用程序中添加一个开关或按钮来启用或禁用推送通知。

它可以做到,因为我在foursqure iphone 应用程序中看到它做到了。他们在设置呼叫通知设置中有一个部分,用户可以为应用启用或禁用不同类型的通知。

我在网上寻找合适的解决方案,但仍然没有找到方法。任何人都可以请任何想法如何做到这一点?

提前致谢:)

【问题讨论】:

  • 苹果允许这样做吗?
  • @Anil 我也在寻找同样的东西,但仍然没有得到任何明确的答案。

标签: ios swift push-notification unusernotificationcenter usernotifications


【解决方案1】:

[仅供参考 - 很少有用户报告它在 iOS 10 上停止工作]

您可以通过再次分别调用registerForRemoteNotificationTypesunregisterForRemoteNotificationTypes 在您的应用程序中轻松启用和禁用推送通知。这个我试过了,效果很好。

【讨论】:

  • 苹果允许这样做吗?
  • 这仍然取决于通知中心的设置,对吧?
  • 正如@TiagoAlmeida 所说,它不再适用于iOS10,Apple 也不推荐。他们在他们的文档中说 unregisterForRemoteNotificationTypes 应该只在极少数情况下使用,比如完全禁用您应用的通知
【解决方案2】:

第一件事是你在应用程序内部can not enable and disablepush notification。如果您发现某些应用程序可以做到这一点,则必须有解决方法。

如果您想在应用程序内部进行操作,则使用一个标识符并根据推送通知启用和禁用按钮将其发送到服务器。因此,您的服务器端编码使用此标识符并根据该标识符工作。就像标识符一样,它是启用的,否则您的服务器将发送通知,否则不会。

您可以使用以下代码检查用户设置enabledisable Push Notifications

启用或禁用 iPhone 推送通知

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) 
 // Yes it is..

希望对你有所帮助..

【讨论】:

  • 正如下面的答案所述,调用 unregisterForRemoteNotificationTypes 将停止推送通知 - 我已经在用户注销时在我的应用程序上对此进行了测试,它确实有效。
【解决方案3】:

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

启用推送通知:

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

【讨论】:

  • 所以您的意思是说您的应用程序设置中有一个启用/禁用按钮,并且您正在使用此代码启用和禁用通知,对吗?它在工作吗?苹果同意吗?
【解决方案4】:

如果您使用FireBase 向您的设备发送推送通知,您可以使用主题订阅在订阅的设备中启用推送通知,并在您不希望用户在这些设备中接收推送通知时取消订阅用户的主题已取消订阅的设备。

要让用户订阅某个主题,只需导入 Firebase,然后使用此方法:

Messaging.messaging().subscribe(toTopic: "topicName")

并取消订阅用户使用:

Messaging.messaging().unsubscribe(fromTopic: "topicName")

【讨论】:

    猜你喜欢
    • 2012-05-16
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    相关资源
    最近更新 更多