我有一个iPhone应用程序,可以接收推送通知。目前,我可以通过转到iPhone的设置/通知来禁用我的应用程序的推送通知。
但是我想在我的应用程序中添加一个开关或按钮来启用或禁用推送通知。
这是可以做到的,因为我在foursqure iPhone应用程序中看到了它。他们在设置中有一个称为通知设置的部分,用户可以为应用程序启用或禁用不同类型的通知。
我搜遍了整个网络,但仍然没有找到适当的解决方案。请问有人可以提供任何想法如何实现吗?
提前感谢 :)
我有一个iPhone应用程序,可以接收推送通知。目前,我可以通过转到iPhone的设置/通知来禁用我的应用程序的推送通知。
但是我想在我的应用程序中添加一个开关或按钮来启用或禁用推送通知。
这是可以做到的,因为我在foursqure iPhone应用程序中看到了它。他们在设置中有一个称为通知设置的部分,用户可以为应用程序启用或禁用不同类型的通知。
我搜遍了整个网络,但仍然没有找到适当的解决方案。请问有人可以提供任何想法如何实现吗?
提前感谢 :)
[提示 - 有少数用户报告说在iOS 10上停止工作]
您可以通过调用registerForRemoteNotificationTypes
和unregisterForRemoteNotificationTypes
函数来轻松启用和禁用应用程序的推送通知。我尝试过这个方法,它是有效的。
首先,你不能在应用内部启用或禁用推送通知。如果你发现某些应用程序可以这样做,那么一定有解决方法。
例如,如果你想在应用内部实现这个功能,可以使用一个标识符,并根据推送通知的启用和禁用按钮将其发送到服务器。因此,你的服务器端编码会根据这个标识符进行操作。例如,如果标识符是“启用”,则你的服务器将发送通知,否则不发送。
你可以使用以下代码检查用户设置的推送通知是否启用或禁用。
启用或禁用 iPhone 推送通知
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone)
// Yes it is..
希望这能帮到你。
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()
首先检查权限,然后根据情况进行更改。
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
}
}
}
FireBase
向设备发送推送通知,则可以使用主题订阅来在订阅的设备上启用推送通知,并在不希望用户在已取消订阅的设备上接收推送通知时从主题中取消订阅用户。Messaging.messaging().subscribe(toTopic: "topicName")
取消订阅用户请使用:
Messaging.messaging().unsubscribe(fromTopic: "topicName")