首页 > 解决方案 > iOS - 在前台接收 FCM 推送消息

问题描述

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

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

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

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

标签: iosswiftfirebasefirebase-cloud-messagingswift4

解决方案


嘿,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 ?? "")")

}

}

推荐阅读