简体   繁体   中英

dont get notification when my app is kill

when my app is in background or foreground i can get notification but when my app is kill . i can't get notification . i use of fcm ... this is my payload that call via postman .

{
"registration_ids":
    ["...."],

  "notification":{  
     "sound":"default",
     "title":"testNotif",
     "body":"welcome in the shop Apple owner"
  }

}

This is my appdelegate :

import UIKit
import UserNotifications

import Firebase
import FirebaseInstanceID
import FirebaseMessaging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    if #available(iOS 10.0, *) {
        let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_,_ in })

        UNUserNotificationCenter.current().delegate = self

        FIRMessaging.messaging().remoteMessageDelegate = self

    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types:  [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

    application.registerForRemoteNotifications()

    FIRApp.configure()

    return true
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {


}

func application(_ application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    FIRInstanceID.instanceID().setAPNSToken(deviceToken as Data, type: FIRInstanceIDAPNSTokenType.prod)
}

func tokenRefreshNotification(notification: NSNotification) {
    if let refreshedToken = FIRInstanceID.instanceID().token() {
        print("InstanceID token: \(refreshedToken)")
    }

    connectToFcm()
}

func connectToFcm() {
    FIRMessaging.messaging().connect { (error) in
        if (error != nil) {
            print("Unable to connect with FCM. \(error)")
        } else {
            print("Connected to FCM.")
        }

        if let refreshedToken = FIRInstanceID.instanceID().token() {
            Common.tokenFCM = refreshedToken
            print("InstanceID token: \(refreshedToken)")
        }
    }
}

func applicationDidBecomeActive(_ application: UIApplication) {
    connectToFcm()
}

}


@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {


    private func userNotificationCenter(center: UNUserNotificationCenter,
                                willPresentNotification notification: UNNotification,
                                withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo
        print("<<<<<<< %@", userInfo)
    }
}

extension AppDelegate : FIRMessagingDelegate {
    func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
        print(">>>>> %@", remoteMessage.appData)

    }
}

Hi use this json format

{

  "registration_ids":
    ["...."],
  "priority":"high",

  "notification":{  
     "sound":"default",
     "title":"testNotif",
     "body":"welcome in the shop Apple owner"
  }

}

There is a problem with priority in this case. If you are not setting it means at normal priority, it may have chance that notification not appears immediately. If you are setting priority to high that means you receive notification immediately. may be because of the way iOS bundles notifications handles it like that.

So what does High Priority do: (Instant messaging)

Attempts to deliver messages immediately and simply makes a network connection with your app server as soon as notification appears to your device. But one more thing you need to consider is power consumption of device. High priority consume more battery than normal one.

For more info and details, you can check Firebase: Setting the priority of a message and Apple Doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM