Push notification not receiving in iOS swift?

I am using FCM

push notifications. FCM

connected, the device is registered successfully and I can print the device token, but the device does not receive notification. In general -> features tab -> enabled push notification and remote notification in reverse land mode.

The device for remote notification is registered here.

  func application(
    _ application: UIApplication,
    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
    ) {

    let trimEnds:String! = {
        deviceToken.description.trimmingCharacters(
            in: CharacterSet(charactersIn: "<>"))
    }()


    let cleanToken:String! = {
        trimEnds.replacingOccurrences(of: " ", with: "")
    }()

    let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
    print(token)

    UserDefaults.standard.set(token, forKey: "deviceToken")
    UserDefaults.standard.synchronize()

    #if DEBUG
    //For Firebase
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.sandbox)
    #else
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.prod)
    #endif

    print("Device Token:", token)


}

      

Here I called the method didReceiveRemoteNotification

to receive notifications on the registered device:

   func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    print("-=-=-=-=-\nDid receive notification\n-=-=-=-",userInfo)
    print("-=-=-=-=-\n")

       NotificationCenter.default.post(name: Notification.Name(rawValue: "notification_recieved"), object: nil)

    if userInfo.index(forKey: "chat_id") != nil {
        print("messag push")
        if (AppUtility?.hasValidText(User.userID))! {
            //let friendImage = userInfo["path"] as! String
            let chatID = userInfo["chat_id"] as! String
            let friendId = userInfo["to"] as! String
            let unRead = userInfo["unread"] as! String
            print(unRead)
            UnReadMsgs = unRead

            let dictAPS = userInfo["aps"] as! NSDictionary
            let dict = dictAPS["alert"] as! NSDictionary
            let friendName = dict["title"] as! String
            let friendMsg = dict["body"] as! String

            if(UIApplication.shared.applicationState == UIApplicationState.active){

                print("app is Active")
                if let wd = self.window {
                    var VC = wd.rootViewController
                    if(VC is UINavigationController){
                        VC = (VC as! UINavigationController).visibleViewController
                        if(VC is ChatViewController!){
                            print("Chat Screen")
                            let chatVC : ChatViewController = VC as! ChatViewController
                            if chatVC.chatId == chatID{
                                print("Same Chat")
                                self.clearChatWithChatID(chatID)
                            }else{
                                CustomNotificationView.showNotificationPopUp(self.window!, name: friendName, msg: friendMsg, image: "", chat: chatID, friendID: friendId)

                                playSound()
                                print("Other Chat")
                            }
                        }else{
                            let nc = NotificationCenter.default
                            nc.post(name: Notification.Name(rawValue: "MessageGet"),
                                                    object: nil,
                                                    userInfo: ["unRead":unRead,
                                                        "date":Date()])
                            CustomNotificationView.showNotificationPopUp(self.window!, name: friendName, msg: friendMsg, image: "", chat: chatID, friendID: friendId)

                            playSound()
                            print("Other Screen")
                        }
                    }
                }

            }else{

                print("app is in BG")

                var vc:ChatViewController!
                vc = ChatViewController(nibName: "ChatViewController", bundle: nil)
                vc.chatId = chatID
                vc.otherUserId = friendId
                vc.otherUserName = friendName
                vc.channelRef = self.channelRef.child("\(chatID)")
                vc.friendImageLink = "\(resourceUrl)\("")"

                let nav = UINavigationController(rootViewController: vc)
                nav.isNavigationBarHidden = false

                if let wd = self.window {
                    var VC = wd.rootViewController
                    if(VC is UINavigationController){
                        VC = (VC as! UINavigationController).visibleViewController
                    }


                    VC!.present(nav, animated: false, completion: {
                    })

                }


            }


        }

    }else{
        let val = userInfo["aps"] as! [String:AnyObject];
        let alert  =   NSString(string: val["alert"] as! String)
        if(UIApplication.shared.applicationState == UIApplicationState.inactive || UIApplication.shared.applicationState == UIApplicationState.background)
        {
            showUserInfo(application, didReceiveRemoteNotification: userInfo)
                      }
        else
        {

            print("top most vc \(String(describing: UIApplication.shared.keyWindow!.rootViewController!.topMostViewController().presentingViewController)) and presentedvc \(String(describing: UIApplication.shared.keyWindow!.rootViewController!.topMostViewController().presentedViewController))")

            if UIApplication.shared.keyWindow!.rootViewController!.topMostViewController() is NYAlertViewController{

                let newAlert = AppUtility?.getDisplayAlertController(title: "FitFlow", messageText: alert as String)
                let nvVc = UIApplication.shared.keyWindow!.rootViewController!.topMostViewController().presentedViewController
                nvVc?.present(newAlert!, animated: true, completion: nil)

                return

            }

                AppUtility?.displayAlert(title:"FitFlow", messageText: alert as String,UIApplication.shared.keyWindow!.rootViewController!.topMostViewController())




        }

    }
}

      

I tested by keeping breakpoints, it is not called a method at all didReceiveRemoteNotification

. How do I get a push notification using the above method?

+2


source to share


2 answers


I've also been stuck with this before.

This requires an FCM token , not an APNS token .

To do this, your AppDelegate class must have these -

import Firebase
import UserNotifications
import FirebaseMessaging

class AppDelegate: UIResponder,
                   UIApplicationDelegate,
                UNUserNotificationCenterDelegate,
                FIRMessagingDelegate {

                }

      

  • Then sign the messaging delegate in didFinishLaunchingWithOptions

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

    GIDSignIn.sharedInstance().clientID =
    "Your client id"
    DispatchQueue.main.async {
        FIRApp.configure()
    }

    FIRMessaging.messaging().remoteMessageDelegate = self


    if #available(iOS 10, *) {
        UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
        application.registerForRemoteNotifications()
    }
        // iOS 9 support
    else if #available(iOS 9, *) {
        UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
        UIApplication.shared.registerForRemoteNotifications()
    }
        // iOS 8 support
    else if #available(iOS 8, *) {
        UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
        UIApplication.shared.registerForRemoteNotifications()
    }
        // iOS 7 support
    else {
        application.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
    }


    return true
}

      

// You don't need the didRegisterForRemoteNotificationsWithDeviceToken method anymore



 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { }

      

the token you received from the delegate didRegisterForRemoteNotificationsWithDeviceToken

is not useful,

You need to use FIRInstanceID.instanceID (). token ()

Add the below code to your applicationDidBecomeActive, this will check for FCM token updates and handle it elegantly.

func applicationDidBecomeActive(_ application: UIApplication) {

    NotificationCenter.default.addObserver(self, selector:
        #selector(tokenRefreshNotification), name:
        NSNotification.Name.firInstanceIDTokenRefresh, object: nil)
}

@objc func tokenRefreshNotification(notification: NSNotification) {

    if let refreshedToken = FIRInstanceID.instanceID().token() {
        print("InstanceID token: \(refreshedToken)")
        UserDefaults.standard.set(refreshedToken, forKey: "deviceToken")
        self.sendFCMTokenToServer(token: refreshedToken)
    }
    /* 
    Connect to FCM since connection may
    have failed when attempted before having a token.
    */
    else { 
        connectToFcm()
    }
}

func updatePushNotificationWebservice() {
    /*
     if you want to save that token on your server 
     Do that here.
     else use the token any other way you want.
    */
}

func connectToFcm() {

        FIRMessaging.messaging().connect { (error) in
            if (error != nil) {
                print("Unable to connect with FCM. \(String(describing: error))")                   
            }
            else {
                print("Connected to FCM.")
                /*
                **this is the token that you have to use**
                print(FIRInstanceID.instanceID().token()!)
                if there was no connection established earlier,
                you can try sending the token again to server.
                */

                let token = FIRInstanceID.instanceID().token()!
                self.sendFCMTokenToServer(token: token)

            }
        }
}

      

For debugging, use the token obtained from FIRInstanceID.instanceID().token()!

and use firebase push-alert console with the same token in the tab> project> Cloud messaging.

https://console.firebase.google.com/u/0/

+2


source


Overriding priority over source code solves my problem.



Please check with firebase console-> cloud messaging (down left) to send push notification.

0


source







All Articles