【问题标题】:How to run the function without showing the push message when it is sent?如何在发送时不显示推送消息的情况下运行该功能?
【发布时间】:2019-11-01 05:17:20
【问题描述】:

我有一个问题需要通过推送消息来解决。如果我向Firebase发送推送消息,我会很好地收到它,我会很好地看到消息。

当我点击推送消息时,AppDelegate文件中的userNotificationCenter函数被执行,函数中的动作列表被执行。

我可以在不接收特定消息并显示消息的情况下执行功能吗?

我目前在哪里接收和处理推送消息。

    @available(iOS 10, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let data = response.notification.request.content.userInfo

        guard
            let push = data[AnyHashable("push")] as? String,
            let getdata = data[AnyHashable("getdata")] as? String,
            let pushdata = data[AnyHashable("pushdata")] as? String
            else {
                print("it's not good data")
                return
        }
        print(push)
        print(getdata)
        print(pushdata)
}

   @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    {
        completionHandler([.alert, .badge, .sound])
    }

我的目的是在从 Firebase 发送特定消息(Ex: push = "noShowPush") 时执行该功能而不向用户显示推送消息。

编辑

我试过"content_available": true 但获取日志6.10.0 - [Firebase/Messaging][I-FCM002019] FIRMessaging received data-message, but FIRMessagingDelegate's-messaging:didReceiveMessage: not implemented

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

        self.window = UIWindow(frame: UIScreen.main.bounds)
        // Override point for customization after application launch.
        //create the notificationCenter
        Messaging.messaging().delegate = self
        FirebaseApp.configure()

        //Register App For Push Notification
        //        self.registerAppForPushNotificaition()
        let center = UNUserNotificationCenter.current()
        let inviteCategory = UNNotificationCategory(identifier: "Notification", actions: [], intentIdentifiers: [], options: UNNotificationCategoryOptions.customDismissAction)
        let categories = NSSet(objects: inviteCategory)

        center.delegate = self
        center.setNotificationCategories(categories as! Set<UNNotificationCategory>)
        DispatchQueue.main.async(execute: {
            UIApplication.shared.registerForRemoteNotifications()
        })
        application.registerForRemoteNotifications()
        self.updateAppViewUI()
        self.window?.makeKeyAndVisible()
        return true
 }
   func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
        // 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)

        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }

        // Print full message.
        print(userInfo)
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        Messaging.messaging().appDidReceiveMessage(userInfo)
        if Auth.auth().canHandleNotification(userInfo) {
            completionHandler(UIBackgroundFetchResult.noData)
            return
        }
        completionHandler(UIBackgroundFetchResult.newData)
    }

我已经有了Messaging.messaging().delegate = self

Firebase 中的日志已经警告你将FirebaseAppDelegateProxyEnabled 设置为No,所以我添加到Info.list

Firebase 日志更改[Firebase/Core][I-COR000003] The default Firebase app has not yet been configured. Add '[FIRApp configure];' ('FirebaseApp.configure()' in Swift) to your application initialization. Read more:

我正在修改的事情进展顺利吗?

我该如何解决这个问题?

【问题讨论】:

  • 您真正想要达到的目标是什么?您的问题似乎不清楚。
  • 我的问题很难理解吗?我的目的是在从 Firebase 发送特定消息(例如:push = "noShowPush")时执行该功能而不向用户显示推送消息。
  • @FrankvanPuffelen 我认为您可以在 firebase 中解决问题。你能帮我解决我的问题吗?

标签: ios swift firebase push-notification


【解决方案1】:

我解决了这个问题。这是我计算出来的顺序:

  1. 发送推送时写"content_available": true"。并且不要 包括titlebody
  2. 当我收到这些错误日志时,6.10.0 - [Firebase/Messaging][I-FCM002019] FIRMessaging received data-message, but FIRMessagingDelegate's-messaging:didReceiveMessage: not implemented
  3. Firebase 中的日志已警告您设置 FirebaseAppDelegateProxyEnabled 为否,所以我添加到 Info.list.

  4. 并改变了函数的执行顺序。

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
        FirebaseApp.configure() // Let it run first.
        Messaging.messaging().delegate = self
        self.window = UIWindow(frame: UIScreen.main.bounds)
        ...
    
  5. 我在 pod 上安装了 'Firebase/Analytics'pod 'Firebase/Analytics'

  6. 继承了MessagingDelegate并实现了 Messaging函数。

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("fcmToken \(fcmToken)")
    }
    
    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
        Log.Info("remort \(remoteMessage.appData)")
        let userInfo = remoteMessage.appData
    }
    

此配置允许您在发送推送消息时将推送消息数据接收到messaging 函数中。这是一条“contentAvailable”值为True 且没有titlebody 的推送消息。

【讨论】:

    猜你喜欢
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 2018-01-31
    • 2022-01-04
    相关资源
    最近更新 更多