【发布时间】:2019-08-12 08:00:54
【问题描述】:
各位开发者您好。
我正在尝试使用 UISwitch 创建一个操作,该操作允许用户禁用/启用他们的推送通知。当我关闭开关时,我不再收到推送通知。但是,如果我尝试重新打开它,我仍然没有收到它。也使用 Firebase。
这是我的应用委托代码。我会尝试分享相关代码,但如果您需要,请随时请求更多代码。
PS:我用于切换器的代码实际上来自我在 StackOverFlow 上找到的答案。
// App delegate
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("Registered for notifications", deviceToken)
Messaging.messaging().apnsToken = deviceToken
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Registered with FCM with token:", fcmToken)
}
private func attemptRegisterForNotifications(application: UIApplication) {
// APNS means Apple push notification services
print("Attempting to register APNS...")
Messaging.messaging().delegate = self
UNUserNotificationCenter.current().delegate = self
// user notification auth
// all of this works for iOs 10+
let options: UNAuthorizationOptions = [.alert, . badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: options) { (granted, error) in
if let error = error {
print("Failed to request auth:", error)
return
}
if granted {
print("Auth granted.")
} else {
print("Auth denied")
}
}
application.registerForRemoteNotifications()
}
这是我在设置视图控制器中的代码
self.notificationSwitch.addTarget(self, action: #selector(action(sender:)), for: .valueChanged)...
@objc func action(sender: UISwitch) {
let userDefaults = UserDefaults.standard
userDefaults.set(sender.isOn, forKey:"identifier")
if(notificationSwitch.isOn) {
print("Switch is on")
UIApplication.shared.registerForRemoteNotifications()
} else {
print("Switch is off")
UIApplication.shared.unregisterForRemoteNotifications()
}
}
【问题讨论】:
-
尝试从您的服务器/firebase 以及在您的切换操作中删除设备令牌。
-
@charanjitsingh 好的,我试试看!
标签: ios swift firebase push-notification