首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在发送时不显示推送消息而运行该函数?

如何在发送时不显示推送消息而运行该函数?
EN

Stack Overflow用户
提问于 2019-11-01 05:17:20
回答 1查看 177关注 0票数 1

我有个问题要用推消息来解决。如果我向Firebase,发送一条推送消息,我将很好地接收到它,并且我会很好地看到消息。

当我单击push消息时,将执行AppDelegate文件中的AppDelegate函数,并执行该函数中的操作列表。

我可以在不接收特定消息和显示消息的情况下执行函数吗?

我现在正在接收和处理推送消息。

代码语言:javascript
运行
复制
    @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")时,在不向用户显示推送消息的情况下执行函数。

EDit

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

代码语言:javascript
运行
复制
 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:

我正在复习的东西是对的吗?

我怎么能解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-04 06:08:02

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

当您发送push "content_available": true"时,

  1. 编写。当我得到这些错误日志时,不要包括body.
  2. And和title,Firebase中的6.10.0 - [Firebase/Messaging][I-FCM002019] FIRMessaging received data-message, but FIRMessagingDelegate's-messaging:didReceiveMessage: not implemented
  3. The日志已经警告您将FirebaseAppDelegateProxyEnabled设置为No,所以我将其添加到Info.list.

  1. 并更改了函数的执行顺序。

func应用程序(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: UIApplication.LaunchOptionsKey: Any?) -> Bool { FirebaseApp.configure() //让它先运行。Messaging.messaging().delegate = self self.window =UIWindow(框架: UIScreen.main.bounds) .

  • 和我在吊舱上安装了'Firebase/Analytics'pod 'Firebase/Analytics'

  • and继承了MessagingDelegate并实现了Messaging函数。

func消息传递(消息传递:消息传递,didReceiveRegistrationToken fcmToken: String) { print("fcmToken (fcmToken)") } func消息传递(消息传递:消息传递,didReceive remoteMessage: MessagingRemoteMessage) { Log.Info("remort ( remoteMessage.appData )")让userInfo =remoteMessage.appData }

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58654420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档