当用户首次调用以下方法时,我在应用程序中使用firebase推送通知。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//Firebase Configure
FirebaseApp.configure()
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
Messaging.messaging().delegate = self
application.registerForRemoteNotifications()
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
// TODO: If necessary send token to application server.
defaultValues.set(fcmToken, forKey: "FCMToken")
defaultValues.synchronize()
}
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
// Change this to your preferred presentation option
completionHandler([.alert])
}
}
// MARK: - Push Notification MessagingDelegate methods.
extension AppDelegate: MessagingDelegate{
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
// TODO: If necessary send token to application server.
defaultValues.set(fcmToken, forKey: "FCMToken")
defaultValues.synchronize()
//Call Sending device info to api.
}
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
defaultValues.set(fcmToken, forKey: "FCMToken")
defaultValues.synchronize()
//Call Sending device info to api.
Helper.shared.appDelegate.SendingDeviceInfo()
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Message data:", remoteMessage.appData)
}
}
当用户按下登录按钮时,我点击了api。 但是我的问题是,当用户注销并再次登录同一API时再次命中,但是使用了nil fcm令牌。 在注销时,我清除所有默认数据。 我们如何解决此问题,对我没有任何帮助。
这是我的注销代码
func loggout(){
//logout and reset root controller - Delete all userdefaults data.
SVProgressHUD.dismiss()
//Clear All Default values
let domain = Bundle.main.bundleIdentifier!
defaultValues.removePersistentDomain(forName: domain)
defaultValues.synchronize()
setRootControllerBeforeLogin()
}
我使用此方法将fcm令牌发送到服务器,在登录按钮操作中调用此方法。
func SendingDeviceInfo(){
let fcmvalue = defaultValues.string(forKey: "FCMToken") ?? ""
let param:[String:Any] = ["registerID":fcmvalue]
WebService.shared.apiDataPostMethod(url: deviceInfoURL, parameters: param) { (response, error) in
if error == nil
{
print(param)
}else{
// print(error?.localizedDescription)
}
}
}
答案 0 :(得分:0)
didReceiveRegistrationToken。
您的问题-注销时清除UserDefaults,因此“ FCMToken”的值为nil
解决方案-1.首先无需从UserDefaults中清除“ FCMToken”,因为下次启动应用程序时会获得相同的令牌(并非总是如此)。Firebase令牌可以随时更改,但不会很快更改。还有一件事情,如果您安装该应用并首次运行,它将始终生成新令牌。
修改 您可以先从UserDefaults获取令牌的值,然后再将其删除并稍后再分配。
let token = UserDefaults.standard.value(forKey: "FCMToken")
if let appDomain = Bundle.main.bundleIdentifier {
UserDefaults.standard.removePersistentDomain(forName: appDomain)
}
UserDefaults.standard.set(token, forKey: "FCMToken")