3
votes

I am trying to implement push notification via Firebase Cloud Messaging to my iOS app. I can set the firebase console and APN perfectly, I can get the notification that sent via Firebase console in my device.

but, when I get the notification, it just shows the alert, no sound, no number in the badge, even though I have stated UNAuthorizationOptions = [.alert, .badge, .sound] here is the code I use in the app delegate

import UIKit
import Firebase
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {

    var window: UIWindow?


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

        FirebaseApp.configure()


        if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate

            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }

        application.registerForRemoteNotifications()

        Messaging.messaging().delegate = self
        let token = Messaging.messaging().fcmToken
        print("FCM token: \(token ?? "")")








        return true
    }



    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String){
        print("Firebase registration token: \(fcmToken)")

    }






}

I also set "FirebaseAppDelegateProxyEnabled" to YES in my Info.plist. and here is my podfile

# Uncomment the next line to define a global platform for your project
 platform :ios, '9.0'

target 'Firebase Push Notification' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for Firebase Push Notification

    pod 'Firebase/Core'
    pod 'Firebase/Messaging'

end

so how to add the sound and the badge when I receive the notification?

3

3 Answers

6
votes

You need to tell your service/backend owner to send payload for the notification similar to this one. Basically you need to have the badge and sound keys for it to work as you expect:

{  
   "aps":{  
      "alert":"This is a message",
      "badge":1,
      "sound":"default"
   }
}
5
votes

by default, if you send the message from Firebase Console, by default the badge and the sound are disabled, please open "Advanced Options" in the bottom of "compose message" in Firebase console, and "enable" the badge and the sound like picture below.

enter image description here

0
votes

I think it would be better for you to handle the badge in your app right when you receive the notification, in this way you can set it to any number depending on the data and the current badge count before getting the notification