Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

swift - IOS Firebase Push notification not working in background mode

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

enter image description here

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add "content-available" with value 1

Example:

{
    "aps" : {
        "content-available" : 1
    },
    "acme1" : "bar",
    "acme2" : 42
}

For more information about background notifications, see the Apple documentation:

https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html

p.s I don't have much reputation for comments, so I'll try to help with the answers


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...