I’m engaged on a chat app with the assistance of Firebase Realtime Database. When any person ship me msg it ought to replace supply report like WhatsApp, so i replace supply node on specific msgid.
So i’ve implement silent push notification for this, at any time when msg obtain in didRecieveRemoteNotification
delegate the background process invoke. My code right here.
func utility(_ utility: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult)
-> Void) {
//extracting right here pathStr and chatId from payload
if (backgroundTask == UIBackgroundTaskIdentifier.invalid) {
print("UIBackgroundTaskIdentifier invalid")
print("UIBackgroundTaskIdentifier beginBackgroundTask")
backgroundTask = UIApplication.shared.beginBackgroundTask { () -> Void in
self.markAsDeliverChat(pathStr: pathStr, chatID: chatId)
UIApplication.shared.endBackgroundTask(self.backgroundTask)
self.backgroundTask = UIBackgroundTaskIdentifier.invalid
}
}
completionHandler(UIBackgroundFetchResult.newData)
}
func markAsDeliverChat(pathStr: String, chatID: String) {
let FIREBASE_CHAT_URL = "https://xxxxxxxxxxxx-default-rtdb.firebaseio.com/"
let database = Database.database(url: FIREBASE_CHAT_URL).reference()
let chatRef = database.youngster("Chat/(pathStr)")
let question = chatRef.queryOrdered(byChild: "id").queryEqual(toValue: chatID)
print("FIREBASE_CHAT_URL = ", FIREBASE_CHAT_URL)
question.observeSingleEvent(of: .worth) { (snapShot) in
guard let worth = snapShot.worth as? NSDictionary else {
print("chat id not discovered")
return
}
print("not reaching right here")
/// HERE is all logic
}
}
Above Perform is working tremendous for Foreground Mode, however when i’m in background mode, the code doesn’t enter in question.observeSingleEvent
. Kindly assist me. I caught right here since 3 days.