繁体   English   中英

Firebase 云消息通知正在控制台中接收但未显示在手机中 - Swift 应用程序

[英]Firebase Cloud Message Notification being received in Console but not being displayed in phone - Swift App

因此,我正在尝试使用 Firebase 函数为我的应用程序中的消息中心实现云消息传递。 我的 firebase function 说它已经在工作,我确实在控制台日志中收到了消息,但是,通知没有在我的手机中以后台模式部署。

一旦我的用户在主视图中登录,我已经实现了接收通知的权限。 这是我的代码:

 func askNotificationPermission() {
        
        //Firebase Push Notifications
          UNUserNotificationCenter.current().delegate = self

          let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
          UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in
                print("HOLA TE DIERON PERMISO")
          })
        
            Messaging.messaging().shouldEstablishDirectChannel = true
        
          UIApplication.shared.registerForRemoteNotifications()
          Messaging.messaging().delegate = self
  
    }

我已经在didFinishLaunchingApplication中实现了必要的 Firebase 函数:

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    //MARK: Firebase
    FirebaseApp.configure()
    
    //Firebase Cloud Messaging
      Messaging.messaging().delegate = self
    
    
    //MARK: Others
    //KeyboardManager
    IQKeyboardManager.shared.enable = true
    
    //navbar
    let navControl = UINavigationBar.appearance()
    let myColor : UIColor = UIColor(red: 0, green: 0.1333, blue: 0.3176, alpha: 1)
    navControl.tintColor = myColor

    return true
}

这是我对 App Delegate 中通知的扩展:

    extension AppDelegate: UNUserNotificationCenterDelegate, MessagingDelegate {
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        Messaging.messaging().shouldEstablishDirectChannel = true
    }
    
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
           
           let dataDict: [String: String] = ["token": fcmToken]
           NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
           
           //TODO: If necessary send token to application server
           //Note: This callback is fired at each app startup and whenever a new token is generated
       }
    
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        
        Messaging.messaging().appDidReceiveMessage(userInfo)
        
        if let messageID = userInfo[gcmMessageIDKey] {
           print("Message ID: \(messageID)")
         }

         // Print full message.
         print("USER INFO: ",userInfo)
    }
    
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print(userInfo)
        completionHandler(UIBackgroundFetchResult.newData)
        print("USER INFO IN DID RECEIVE REMOTE NOTIFICAITON:", userInfo, UIBackgroundFetchResult.newData)
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        
        let userInfo = notification.request.content.userInfo as! [String:Any]

        completionHandler([.alert, .badge, .sound])
        print("HOLI")

    }
}

我不知道可能是什么问题以及为什么我在控制台中收到信息但它没有被推送到应用程序。 我已经搜索了几个 stackoverflow 问题,但没有一个有效。

这是我的控制台记录的内容:

2020-08-06 11:06:06.804344-0500 FleaMarket_UserSide[13985:4429211] [] nw_read_request_report [C2] 接收失败,出现错误“软件导致连接中止” [AnyHashable("title"): Jay Shetty Meditations, AnyHashable("aps "): { "内容可用" = 1; },AnyHashable(“gcm.message_id”):1596729966264394,AnyHashable(“message”):你明白了吗?,AnyHashable(“google.c.sender.id”):520471783461]用户信息通知:收到AnyHashable("title"): Jay Shetty Meditations, AnyHashable("aps"): { "content-available" = 1; }, AnyHashable("gcm.message_id"): 1596729966264394, AnyHashable("message"): 你明白了吗?, AnyHashable("google.c.sender.id"): 520471783461] UIBackgroundFetchResult 1020-0806 :06.835793-0500 FleaMarket_UserSide[13985:4429216] 6.27.0 - [Firebase/Firestore][I-FST000001] WatchStream (282c75618) Stream 错误:'不可用:套接字未连接'

我发现了我的错误。 它在我的 firebase function 中。 我写错了我的有效载荷。 我曾写过:

var newmessage = { data: { title: groupName, body: message }, topic: offerTopic, };

根据 Firebase 文档,正确的有效载荷是 NOTIFICATION:

var newmessage = {
      notification: {
        title: groupName,
        body: message
      },
      topic: offerTopic,
    };

请务必遵守此处找到的 firebase 云消息文档,否则它不会将其视为通知,也不会推送。

https://firebase.google.com/docs/cloud-messaging/ios/topic-messaging

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM