【发布时间】:2020-10-24 18:56:39
【问题描述】:
我正在开发一个 Swift iOS 14 应用程序,它可以发送和接收来自 Firebase Cloud Messaging 的推送通知。
我从 FCM 发送一条消息,其中包含应用程序必须处理的有效负载,使用有效负载数据更新内部 SQLite 数据库以供以后使用,在视图中显示项目。
当应用程序处于前台时,我在 didReceiveRemoteNotification 方法中收到通知并更新数据库,但是当应用程序处于后台或被终止时,收到通知但没有调用任何方法来处理有效负载并更新数据库.
我已经阅读了很多关于这个问题的主题,但我都没有找到解决方案。
目前我不想使用外部数据库插入数据,稍后再读取外部数据库,但是如果没有其他选项我会更改应用程序(原因是我不想要存储用户应用程序之外的任何信息)。
我的 AppDelegate.swift 如下:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//Firebase Auth + APNs
FirebaseApp.configure()
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
application.registerForRemoteNotifications()
Messaging.messaging().delegate = self
return true
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
UserDefaults.standard.set(fcmToken, forKey: UserConstants.MESSAGING_TOKEN)
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if(userInfo["name"] != nil) {
ContactsService.sharedInstance.addAlert(phoneNumber: userInfo["phone"] as! String, name: userInfo["name"] as! String, isLocalized: Bool(userInfo["isLocalized"] as? String), longitude: (userInfo["longitude"] as! NSString).doubleValue, latitude: (userInfo["latitude"] as! NSString).doubleValue)
}
}
有人可以帮助我,告诉我是否可以这样做,或者是否有必要将数据存储在外部以便以后检索?
谢谢!
【问题讨论】:
标签: ios swift notifications background apple-push-notifications