Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show badge number and sound after receiving push notification via Firebase Cloud Messaging?

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?

like image 812
Alexa289 Avatar asked Dec 19 '22 00:12

Alexa289


1 Answers

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"
   }
}
like image 122
badhanganesh Avatar answered May 28 '23 13:05

badhanganesh