In my application I'm using Firebase to receive notification but I have a problem: when I send a notification from the Firebase console I hear only the vibration of the notification and I can see message body in the log. I can't show the banner notification with the message body as text and an icon.
I follow the official guide from here but it doesn't work.
This is my 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()
}
}
This is my 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]
}
}
How can I do to show notification in a banner?
Thanks in advance.