【问题标题】:iOS - Receiving FCM push messages in foregroundiOS - 在前台接收 FCM 推送消息
【发布时间】:2019-06-28 14:38:58
【问题描述】:

使用 Swift 4 和 Xcode 10, 我想知道是否可以使用 Firebase 推送通知服务(云消息传递)仅在前台模式下接收推送通知(不需要后台)。我想在用户处于前台模式时使用新数据更新用户,而无需用户进行刷新。

问题是我不想请求用户的通知许可,因为他不应该知道我正在为他做的“实时更新”。

我已经设法通过使用称为“直接 FCM 通道消息”的 FCM 功能来实现这一点,但对我来说,问题是它具有某种排队机制,并且当用户处于后台模式然后返回应用程序(前台模式)他得到了他错过的所有更新。我不希望这种“排队机制”发生。

我将不胜感激任何形式的帮助! 谢谢。

【问题讨论】:

    标签: ios swift firebase firebase-cloud-messaging swift4


    【解决方案1】:

    嘿,Eitan Aflalo,这是我朋友面临的同一个问题。

    1> 您必须在推送通知上设置项目设置功能。 2> 使用通知标识符。请按照以下步骤操作。

    我希望这能奏效。

    extension AppDelegate {  
    
    //MARK:- USER NOTIFICATION DELEGATE
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    {
        print(notification.request.content.userInfo)
    
        if notification.request.identifier == "rig"{
            completionHandler( [.alert,.sound,.badge])
        }else{
            completionHandler([.alert, .badge, .sound])
        }
    
    }
    
    
    
    func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    {
        //Handle the notification
        completionHandler(
            [UNNotificationPresentationOptions.alert,
             UNNotificationPresentationOptions.sound,
             UNNotificationPresentationOptions.badge])
    }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    
        Messaging.messaging().apnsToken = deviceToken
        let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
        print(deviceTokenString)
    
    }
    
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    
    }
    
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    
        print(userInfo)
    
        switch application.applicationState {
        case .active:
            let content = UNMutableNotificationContent()
            if let title = userInfo["title"]
            {
                content.title = title as! String
            }
            if let title = userInfo["text"]
            {
                content.body = title as! String
            }
            content.userInfo = userInfo
            content.sound = UNNotificationSound.default()
    
            let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 0.5, repeats: false)
            let request = UNNotificationRequest(identifier:"rig", content: content, trigger: trigger)
    
            UNUserNotificationCenter.current().delegate = self
            UNUserNotificationCenter.current().add(request) { (error) in
                if let getError = error {
                    print(getError.localizedDescription)
                }
            }
        case .inactive:
            break
        case .background:
            break
        }
    
    
    
    }
    
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("i am not available in simulator \(error)")
    }
    
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
        let token = Messaging.messaging().fcmToken
        print("FCM token: \(token ?? "")")
    
    }
    
    }
    

    【讨论】:

    • 感谢您的帮助,但您的解决方案未能成功。也许我错过了一些实现.. 你有完整的代码吗?
    • @Eitan Aflalo 谢谢,但这是处理 FCM 通知的方式,您可以使用 inactivebackground applicationState。我希望这是需要帮助的。 – Pradip Patel 47 分钟前