Mit der neueren Firebase-Cloud-Messaging mit Methode swizzling kann ich eine Nutzlast in der didReceiveRemoteNotification
Methode in meinem App-Delegat erhalten, wenn meine App foregrounded ist. Allerdings bekomme ich keine Art von Nutzlast und didReceiveRemoteNotification
wird nicht aufgerufen, wenn meine App Hintergrund ist, trotz der API-Antwort, die die Nachricht erfolgreich gesendet wird (siehe unten)
Hier ist die Anfrage, die ich an die FCM api senden eine Push-Benachrichtigung https://fcm.googleapis.com/fcm/send
Firebase Cloud Messaging iOS: Keine Hintergrund-Push-Benachrichtigung
{
"to": "{valid-registration-token}",
"priority":"high", //others suggested setting priority to high would fix, but did not
"notification":{
"body":"Body3",
"title":"test"
}
}
mit Antwort von FCM
{
"multicast_id": 6616533575211076304,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1468906545447775%a4aa0efda4aa0efd"
}
]
}
Hier ist mein AppDelegate Code auslösen
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Fabric.with([Crashlytics.self])
FIRApp.configure()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification),
name: kFIRInstanceIDTokenRefreshNotification, object: nil)
return true
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// Let FCM know about the message for analytics etc.
FIRMessaging.messaging().appDidReceiveMessage(userInfo)
// Print full message.
print("%@", userInfo)
// handle your message
// let localNotification = UILocalNotification()
// localNotification.fireDate = NSDate()
// let notification = userInfo["notification"]!
// localNotification.alertBody = notification["body"] as? String
// localNotification.alertTitle = notification["title"] as? String
// localNotification.timeZone = NSTimeZone()
// application.scheduleLocalNotification(localNotification)
}
func tokenRefreshNotification(notification: NSNotification) {
let refreshedToken = FIRInstanceID.instanceID().token()!
print("InstanceID token: \(refreshedToken)")
LoginVC.registerForPushNotifications()
connectToFcm()
}
//foreground messages
func applicationDidBecomeActive(application: UIApplication) {
connectToFcm()
}
// [START disconnect_from_fcm]
func applicationDidEnterBackground(application: UIApplication) {
FIRMessaging.messaging().disconnect()
print("Disconnected from FCM.")
}
func connectToFcm() {
FIRMessaging.messaging().connectWithCompletion { (error) in
if (error != nil) {
print("Unable to connect with FCM. \(error)")
} else {
print("Connected to FCM.")
}
}
}
}
Ich nenne diesen Code zu einem späteren Fluss in meiner App für Berechtigungen
static func registerForPushNotifications() {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
}
fragen Da ich in der Lage bin Benachrichtigung zu erhalten, während App im Vordergrund i nehme an, dies alle Bedenken zerstreuen würde, dass meine APNs-Zertifikate werden nicht hochgeladen oder das Registrierungs-Token ist falsch. Wenn das nicht der Fall ist, bitte Kommentar und ich werde wieder in dieses Kaninchenloch gehen. Es gibt wahrscheinlich etwas einfaches, das ich übersehe, aber wie kann ich die Benachrichtigungen erhalten, während die App im Hintergrund läuft? Danke
Ich bin vor einem ähnlichen Kampf Delegate wird aufgerufen, wenn die Anwendung im Vordergrund ist, aber keine Taskleistenbenachrichtigung angezeigt wird, wenn sich die Anwendung im Hintergrund befindet. Interessant zu sehen, wenn etwas auftaucht. Ich werde dich wissen lassen, ob ich das herausgefunden habe. – Kazimieras
Haben Sie sich dieses [Problem] (https://github.com/firebase/quickstart-ios/issues/21) auf github angesehen? – Kazimieras
Wenn Ihre App im Vordergrund ist, können Sie Nachrichten direkt von FCM empfangen, nicht über APNs. Dies wird verwendet, wenn sich Ihre App im Hintergrund befindet.Um sicherzustellen, dass APNs korrekt konfiguriert sind, können Sie durch die Implementierung der Methode didRegisterForRemoteNotificationsWithDeviceToken bestätigen, dass Sie ein APNs-Token erhalten. –