Skip to content

Instantly share code, notes, and snippets.

@iMostfa
Created February 7, 2020 15:18
Show Gist options
  • Save iMostfa/ab09fe32e403d53e74b71a82fec7d13a to your computer and use it in GitHub Desktop.
Save iMostfa/ab09fe32e403d53e74b71a82fec7d13a to your computer and use it in GitHub Desktop.
Notifications Firebase
//In your app delegate, add the following
static var deviceID = String()
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
guard let newToken = InstanceID.instanceID().token() else {return}
AppDelegate.deviceID = newToken
connectToFCM()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let notification = response.notification.request.content.body
// print(notification)
completionHandler()
}
func application(_ application: UIApplication,
didReceiveRemoteNotification notification: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if Auth.auth().canHandleNotification(notification) {
completionHandler(.noData)
return
}
// This notification is not auth related, developer should handle it.
// handleNotification(notification)
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
InstanceID.instanceID().instanceID { (result, error) in
guard let token = result?.token else {return}
AppDelegate.deviceID = token
UserManager.shared.fcmToken = token //ADDED THIS FROM YOUR CODE
print("DeviceID")
self.connectToFCM()
}
}
func connectToFCM(){
Messaging.messaging().shouldEstablishDirectChannel = true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment