Utilizzo della nuova messaggistica cloud Firebase con metodo swizzling sono riuscito a ricevere un payload nel metodo didReceiveRemoteNotification
nel delegato della mia app quando la mia app è in primo piano. Tuttavia non ottengo alcun tipo di payload e didReceiveRemoteNotification
non viene chiamato quando la mia app è con sfondo, nonostante la risposta API che il messaggio è stato inviato correttamente (vedi sotto)
Ecco la richiesta che invio all'APM FCM per attivare una notifica push https://fcm.googleapis.com/fcm/send
Firebase Cloud Messaging iOS: nessuna notifica push in background
{
"to": "{valid-registration-token}",
"priority":"high", //others suggested setting priority to high would fix, but did not
"notification":{
"body":"Body3",
"title":"test"
}
}
con la risposta di FCM
{
"multicast_id": 6616533575211076304,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1468906545447775%a4aa0efda4aa0efd"
}
]
}
Ecco il mio codice AppDelegate
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.")
}
}
}
}
Io chiamo questo codice in un flusso più avanti nella mia app per chiedere i permessi
static func registerForPushNotifications() {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
}
Dato che io sono in grado di ricevere le notifiche mentre applicazione è in foreground Presumo che ciò sarebbe dissipare tutte le preoccupazioni che i miei APN certificati non vengono caricati o il token di registrazione non è corretto. Se questo non è il caso, si prega di commentare e andrò di nuovo in quella tana del coniglio. Probabilmente c'è qualcosa di semplice che sto trascurando, ma come faccio a far apparire le notifiche mentre l'app è in background? Grazie
che sto affrontando una lotta simile oggi: il gestore in app viene chiamato delegato, quando l'applicazione è in primo piano, ma non viene visualizzata alcuna notifica vassoio, quando l'app è in background. Interessante vedere se succede qualcosa. Ti farò sapere se capisco questo. – Kazimieras
Hai dato un'occhiata a questo [problema] (https://github.com/firebase/quickstart-ios/issues/21) su github? – Kazimieras
Quando l'app è in primo piano, puoi ricevere messaggi direttamente da FCM non tramite APN, che è ciò che viene utilizzato quando l'app è in background.Per essere sicuri che gli APN siano configurati correttamente, puoi confermare che stai ricevendo un token APN implementando il metodo didRegisterForRemoteNotificationsWithDeviceToken. –