Dans mon application, j'utilise Firebase pour recevoir une notification mais j'ai un problème: lorsque j'envoie une notification depuis la console Firebase j'entends seulement la vibration de la notification et je peux voir le corps du message dans le journal. Je ne peux pas afficher la notification de bannière avec le corps du message en tant que texte et une icône. Je suis le guide officiel de here mais cela ne fonctionne pas.Firebase notification
Ceci est mon AppDelegate:
import UIKit
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
FIRApp.configure()
// Add observer for InstanceID token refresh callback.
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) {
// TODO: Handle data of notification
print("This is userInfo -> \(userInfo)")
print("")
print(userInfo["notification"]!["body"])
print("")
FIRMessaging.messaging().appDidReceiveMessage(userInfo)
completionHandler(.NoData)
NSLog("startLocalNotification")
var notification: UILocalNotification = UILocalNotification()
notification.fireDate = NSDate(timeIntervalSinceNow: 7)
notification.alertBody = userInfo["body"] as? String
notification.timeZone = NSTimeZone.defaultTimeZone()
notification.soundName = UILocalNotificationDefaultSoundName
notification.applicationIconBadgeNumber = 5
notification.alertAction = "open"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
func tokenRefreshNotification(notification: NSNotification) {
let refreshedToken = FIRInstanceID.instanceID().token()!
print("InstanceID token: \(refreshedToken)")
// Connect to FCM since connection may have failed when attempted before having a token.
connectToFcm()
}
// [START connect_to_fcm]
func connectToFcm() {
FIRMessaging.messaging().connectWithCompletion { (error) in
if (error != nil) {
print("Unable to connect with FCM. \(error)")
} else {
print("Connected to FCM.")
}
}
}
// [END connect_to_fcm]
func applicationWillResignActive(application: UIApplication) {
}
func applicationDidEnterBackground(application: UIApplication) {
//Uncomment below to disconnect
//FIRMessaging.messaging().disconnect()
//print("Disconnected from FCM.")
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
connectToFcm()
}
}
Ceci est mon ViewController:
import UIKit
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func handleLogTokenTouch(sender: UIButton) {
let token = FIRInstanceID.instanceID().token()
// print("InstanceID token: \(token!)")
print("InstanceID token: \(token)")
}
@IBAction func handleSubscribeTouch(sender: UIButton) {
// [START subscribe_topic]
FIRMessaging.messaging().subscribeToTopic("/topics/news")
print("Subscribed to news topic")
// [END subscribe_topic]
}
}
Comment puis-je faire pour montrer une notification dans une bannière?
Merci d'avance.
Si votre application est fonctionnement ou Au premier plan, l'application appellera simplement la méthode déléguée, AUCUNE alerte ou bannière ne sera affichée. Alerte ou Bannière sera affiché si votre application est en arrière-plan ou est supprimée (pas au premier plan). Pour afficher une bannière pendant que l'application est en cours d'exécution, vous devez écrire votre propre code. Reportez-vous à [ce post] (http://stackoverflow.com/questions/4974512/uilocalnotification-isnt-working-at-all) –
@DipenPanchasara si je tue mon application et que j'envoie une notification de la console rien n'apparaît sur mon appareil – Jigen
Alert ou Banner est géré par OS lui-même, si vous voulez l'afficher commentez votre 'UILocalNotification', cela ne fonctionnera pas. Lorsque votre application n'est pas en cours d'exécution, le système traitera automatiquement la notification et affichera une alerte ou une bannière appropriée. Je crois que vous comprenez le contexte. –