在应用内启用或禁用iPhone推送通知

29

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

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

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

我搜遍了整个网络,但仍然没有找到适当的解决方案。请问有人可以提供任何想法如何实现吗?

提前感谢 :)


苹果公司允许这个吗? - Anil
@Anil 我也在寻找同样的答案,但是目前还没有得到明确的回答。 - shaqir saiyed
5个回答

35

[提示 - 有少数用户报告说在iOS 10上停止工作]

您可以通过调用registerForRemoteNotificationTypesunregisterForRemoteNotificationTypes函数来轻松启用和禁用应用程序的推送通知。我尝试过这个方法,它是有效的。


这是否被苹果允许? - Anil
这仍然取决于通知中心的设置,对吗? - nmr
正如@TiagoAlmeida所说,在iOS10上它已经不再工作,而且也不被苹果推荐使用。他们在文档中指出,unregisterForRemoteNotificationTypes只应该在极少数情况下使用,比如完全禁用你的应用程序的通知。 - Matthias

24

首先,你不能在应用内部启用或禁用推送通知。如果你发现某些应用程序可以这样做,那么一定有解决方法。

例如,如果你想在应用内部实现这个功能,可以使用一个标识符,并根据推送通知的启用和禁用按钮将其发送到服务器。因此,你的服务器端编码会根据这个标识符进行操作。例如,如果标识符是“启用”,则你的服务器将发送通知,否则不发送。

你可以使用以下代码检查用户设置的推送通知是否启用或禁用。

启用或禁用 iPhone 推送通知

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

希望这能帮到你。


2
正如下面的答案所述,调用unregisterForRemoteNotificationTypes将停止推送通知的传递 - 我已经在用户注销时测试了我的应用程序,它确实有效。 - Tony Million

2
实际上,可以通过注册和注销推送通知来启用和禁用推送通知。 启用推送通知:
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()

你的意思是说你在应用程序的设置中有一个启用/禁用按钮,并且你正在使用这段代码来启用和禁用通知,对吗?它有效吗?苹果会批准这个吗? - shaqir saiyed

0

首先检查权限,然后根据情况进行更改。

func checkPermission(completion: @escaping (_ isCameraPermissionOn: Bool) -> ()) {
        let current = UNUserNotificationCenter.current()
        current.getNotificationSettings(completionHandler: { permission in
            switch permission.authorizationStatus  {
            case .authorized:
                //If user allow the permission
                completion(true)
            case .denied:
                //If user denied the permission
                completion(false)
            case .notDetermined:
                //First time
                current.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
                    if granted {
                        completion(true)
                    } else {
                        completion(false)
                    }
                }
            case .provisional:
                // @available(iOS 12.0, *)
                // The application is authorized to post non-interruptive user notifications.
                completion(true)
            case .ephemeral:
                // @available(iOS 14.0, *)
                // The application is temporarily authorized to post notifications. Only available to app clips.
                completion(true)
            @unknown default:
                print("Unknow Status")
            }
        })
    }

调用:

AppsPermissionCheckingManager.shared.checkPermission { isPermissionOn in
            DispatchQueue.main.async {
                if isPermissionOn == true {
                    //It's on
                } else {
                    //It's off
                }
            }
        }

0
如果您正在使用FireBase向设备发送推送通知,则可以使用主题订阅来在订阅的设备上启用推送通知,并在不希望用户在已取消订阅的设备上接收推送通知时从主题中取消订阅用户。
要订阅用户到主题,只需导入Firebase,然后使用此方法:
Messaging.messaging().subscribe(toTopic: "topicName")

取消订阅用户请使用:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接