Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在应用程序内启用或禁用Iphone推送通知_Ios_Swift_Push Notification_Unusernotificationcenter_Usernotifications - Fatal编程技术网

Ios 在应用程序内启用或禁用Iphone推送通知

Ios 在应用程序内启用或禁用Iphone推送通知,ios,swift,push-notification,unusernotificationcenter,usernotifications,Ios,Swift,Push Notification,Unusernotificationcenter,Usernotifications,我有一个iphone应用程序,可以接收推送通知。目前,我可以通过进入iphone设置/通知来禁用应用程序的推送通知 但我想在我的应用程序中添加一个开关或按钮来启用或禁用推送通知 这是可以做到的,因为我在foursqure iphone应用程序中看到了它。他们在设置呼叫通知设置中有一个部分,用户可以为应用程序启用或禁用不同类型的通知 我在网上到处寻找一个合适的解决方案,但仍然没有找到一个方法。有人能告诉我怎么做吗 提前感谢:)首先,您不能在应用程序内部启用和禁用推送通知。若你们发现了一些应用程序,

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

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

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

我在网上到处寻找一个合适的解决方案,但仍然没有找到一个方法。有人能告诉我怎么做吗


提前感谢:)

首先,您不能在应用程序内部启用和禁用推送通知。若你们发现了一些应用程序,那个么一定有解决方案

比如,如果你想在应用程序内部执行此操作,则使用一个标识符,并根据按钮通知启用和禁用按钮将其发送到服务器。因此,您的服务器端编码使用该标识符并根据该标识符工作。Like identifier是说它启用了,否则服务器将发送通知

您可以使用以下代码检查用户设置
启用
禁用
推送通知

启用或禁用Iphone推送通知

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

希望,这将对您有所帮助。

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


通过再次分别调用
registerForRemoteNotificationTypes
unregisterForRemoteNotificationTypes
,您可以轻松地在应用程序中启用和禁用推送通知。我已经尝试过这个方法,而且效果很好。

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

启用推送通知:

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()
}
UIApplication.shared.unregisterForRemoteNotifications()
委托方法

@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
}
禁用推送通知:

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()
}
UIApplication.shared.unregisterForRemoteNotifications()

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

要向用户订阅主题,只需导入Firebase,然后使用以下方法:

Messaging.messaging().subscribe(toTopic: "topicName")
要取消用户订阅,请使用:

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

正如下面的答案所述,调用unregisterForRemoteNotificationTypes将停止推送通知的传递-我在用户注销时在我的应用程序上测试了这一点,它确实有效。苹果允许这样做吗?@Anil me也在寻找同样的方法,但仍然没有得到任何明确的答案。这是苹果公司允许的吗?不过这仍然取决于通知中心的设置,对吗?正如@TiagoAlmeida所说,它不再适用于iOS10,苹果公司也不推荐它。他们在他们的文档中说,unregisterForRemoteNotificationTypes只应在极少数情况下使用,例如完全禁用应用程序的通知。所以你的意思是说,你的应用程序设置中有一个启用/禁用按钮,你正在使用此代码启用和禁用通知,对吗?它起作用了吗?苹果批准了吗?