当前位置: 首页 > 面试题库 >

推送通知已传递,但didReceiveRemoteNotification从未称为Swift

萧秋月
2023-03-14
问题内容

我已经通过FCM在我的两个相关应用程序中成功实现了推送通知,同时尝试实现一些逻辑以在接收到通知时增加徽章编号。

我意识到didReceiveRemoteNotification完全没有调用委托方法,因为我没有得到任何印刷品,但是我确实从willPresentnotification和获得印刷品didReceiveresponse。因此,设置UIApplication.shared.applicationIconBadgeNumberdidFinishLaunchingWithOptions没有任何效果,但在设置它didReceiveresponse一样。

didReceiveRemoteNotification应该调用文档后的内容,但是通知到达时我永远不会从中得到打印结果。

我尝试注释掉整个didReceiveRemoteNotification方法,并且通知仍在传递。

为什么会这样呢?我想我不太了解在此设置中谁在处理消息。你能帮我澄清一下吗?

AppDelegate方法:

didFinishLaunchingWithOptions

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window?.tintColor = UIColor.blue
        // Use Firebase library to configure APIs
        FirebaseApp.configure()
        Messaging.messaging().delegate = self
        Crashlytics().debugMode = true
        Fabric.with([Crashlytics.self])
        // setting up notification delegate
        if #available(iOS 10.0, *) {
            //iOS 10.0 and greater
            UNUserNotificationCenter.current().delegate = self
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            //Solicit permission from the user to receive notifications
            UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { granted, error in
                DispatchQueue.main.async {
                    if granted {
                        print("didFinishLaunchingWithOptions iOS 10: Successfully registered for APNs")
                        UIApplication.shared.registerForRemoteNotifications()
//                        UIApplication.shared.applicationIconBadgeNumber = 1
                        AppDelegate.badgeCountNumber = 0
                        UIApplication.shared.applicationIconBadgeNumber = 0
                    } else {
                        //Do stuff if unsuccessful...
                        print("didFinishLaunchingWithOptions iOO 10: Error in registering for APNs: \(String(describing: error))")
                    }
                }
            })
        } else {
            //iOS 9
            let type: UIUserNotificationType = [UIUserNotificationType.badge, UIUserNotificationType.alert, UIUserNotificationType.sound]
            let setting = UIUserNotificationSettings(types: type, categories: nil)
            UIApplication.shared.registerUserNotificationSettings(setting)
            UIApplication.shared.registerForRemoteNotifications()
//            UIApplication.shared.applicationIconBadgeNumber = 1
            UIApplication.shared.applicationIconBadgeNumber = 0
            print("didFinishLaunchingWithOptions iOS 9: Successfully registered for APNs")
        }
        // setting up remote control values
        let _ = RCValues.sharedInstance
        GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
        Crashlytics().debugMode = true
        Fabric.with([Crashlytics.self])
        //        // TODO: Move this to where you establish a user session
        //        self.logUser()
        var error: NSError?
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        } catch let error1 as NSError{
            error = error1
            print("could not set session. err:\(error!.localizedDescription)")
        }
        do {
            try AVAudioSession.sharedInstance().setActive(true)
        } catch let error1 as NSError{
            error = error1
            print("could not active session. err:\(error!.localizedDescription)")
        }
        // goggle only
        GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
//        GIDSignIn.sharedInstance().delegate = self
        // Facebook SDK
        return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
//        return true
    }

didReceiveRemoteNotification

 // foreground
        func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
            print("didReceiveRemoteNotification: Received new push Notification")
            // If you are receiving a notification message while your app is in the background,
            // this callback will not be fired till the user taps on the notification launching the application.
            // TODO: Handle data of notification

            // With swizzling disabled you must let Messaging know about the message, for Analytics
            Messaging.messaging().appDidReceiveMessage(userInfo)
            AppDelegate.badgeCountNumber += userInfo["badge"] as! Int
            print("AppDelegate.badgeCountNumber is : \(String(describing: AppDelegate.badgeCountNumber))")
//            UIApplication.shared.applicationIconBadgeNumber = AppDelegate.badgeCountNumber
            UIApplication.shared.applicationIconBadgeNumber =  10//AppDelegate.badgeCountNumber
            // Print full message.
            print("didReceiveRemoteNotification: Push notificationMessage is: \(userInfo)")
        }




        // background
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print("didReceiveRemoteNotification with handler : Received new push Notification while in background")
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification

        // With swizzling disabled you must let Messaging know about the message, for Analytics
        Messaging.messaging().appDidReceiveMessage(userInfo)
        if let messageID = userInfo[ userDetails.fcmToken] { // working for looged in
            print("didReceiveRemoteNotification: Message ID: \(messageID)")
        }

        // Print full message.
        print("didReceiveRemoteNotification: Push notificationMessage is: \(userInfo)")
        AppDelegate.badgeCountNumber += userInfo["badge"] as! Int
        print("AppDelegate.badgeCountNumber is : \(String(describing: AppDelegate.badgeCountNumber))")
        UIApplication.shared.applicationIconBadgeNumber +=  userInfo["badge"] as! Int
        completionHandler(UIBackgroundFetchResult.newData)
    }

问题答案:

我终于找到了解决方案。需要"content_available": true在从App2向App1发送功能的发布后的警报定义中设置警报,否则将传递通知,但不会调用’didReceiveRemoteNotification’并且您不能使用’userInfo’。希望这对其他人有帮助,因为我没有找到有关此问题的太多信息。如果您使用Postman或类似工具设置通知,请在此处检查didReceiveRemoteNotification函数是否不使用FCM通知服务器调用,因为这是我发现的唯一关于此问题并解决了我问题的帖子。感谢@Ranjani尝试帮助我。

let postParams: [String : Any] = [
                "to": receiverToken,
                "notification": [
                    "badge" : 1,
                    "body": body,
                    "title": title,
                    "subtitle": subtitle,
                    "sound" : true, // or specify audio name to play
                    "content_available": true, // this will call didReceiveRemoteNotification in receiving app, else won't work
                    "priority": "high"
                ],
                "data" : [
                    "data": "ciao",
            ]
                ]


 类似资料:
  • 我已经试着调试Firebase推送通知相当长时间了,但没有得到任何运气。我相信我已经正确地设置了临时配置文件和APNs证书。当我不包含方法时 当应用程序从Firebase通知控制台发送时,它会在前台接收通知,因为它会被打印出来,但它不会在后台接收通知。

  • 我为沙盒iOS设置了推送通知,它们曾经工作过,但由于某种原因目前不工作。这是来自云观察日志的错误AWS SNS: 这是我的发送代码(AWS Lambda调用此代码): Lambda方法的返回数据: 我已经将问题缩小到不与我的lambda方法关联,因为我试图发布到直接从SNS控制台创建的endpoint。我还想知道为什么这个旧的实现在过去,一个月或两个月前,仍然有效,而现在不再有效。我认为这可能与过

  • full方法使用不同的方法,但值仍然不会传递给mainActivity。总是有一个空的?

  • 我正在写一个iOS的应用程序,使用laravel的API和谷歌Firebase的推送通知。当我使用Firebase云消息传递发送消息推送时,它会进入我的设备。当我使用laravel发送推送通知时,它不会影响。这里是我的脚本发送推送通知laravel: 它返回一个成功的结果,但通知未发送到iOS设备。 PS:它可以在Android设备上成功运行。

  • 我有一些与工作灯中推送通知相关的问题: > 当应用程序关闭且通知消息到达时,我通过单击应用程序图标而不是通知栏中的消息来启动应用程序,在我看来,我无法在应用程序中取回通知消息。(应用程序在后台运行时没有问题)步骤: a)订阅应用程序内部的事件源 b)关闭应用程序 c)向设备提交通知(“Hello”) d)在应用程序中,通知消息(“Hello”)显示在状态栏中。向下滑动状态栏。消息将显示在通知栏中

  • 首先,我想声明我一直在研究推送通知和web通知之间的关系,但我有点困惑。 我从这里读到PWAs的推送通知在Safari上的iOS(iPhone)不起作用:从PWA向iOS发送推送通知 然而,如果iPhone用户使用的是Chrome,这是否意味着它们可以工作呢?或者推送通知在任何浏览器上对iPhone中的PWAs都不起作用? 这就把我带到了web通知。web通知在后台对PWAs起作用吗?我的问题是w