I implement firebase notification and when my device is in foreground notification works fine through firebase console and my backend server too. But in case of background mode my when I send notification through console its working fine but when its sending through my backend server it's not working also try with online tools like push try but not working.
I set auth key in firebase console. and in my app delegate file code is following..
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
....
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
// Messaging.messaging().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
}
another method I added
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("receive new data from didReceiveRemoteNotification")
print(userInfo)
completionHandler(.newData)
}
//MARK:Messaging delegate
extension AppDelegate : MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: (String(describing: fcmToken))")
user.fcmToken = fcmToken
user.firebaseToken = fcmToken
print("user token >>>>> (user.fcmToken)")
}
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
// Print full message.
print("user_info---",userInfo)
// Change this to your preferred presentation option
completionHandler([.alert,.sound])
}
//--- one
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
print("user_info---",userInfo)
print("tap on on forground app",userInfo)
completionHandler()
}
}
my capabilities section in xcode
Notification payload:
{"to":"mytoken","data":{"aps":{"alert":{"title":"Portugal vs. Denmark","body":"great match!"},"badge":1}},"priority":"high"}
question from:
https://stackoverflow.com/questions/65933806/ios-firebase-push-notification-not-working-in-background-mode 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…