iOS-前台接收FCM推送消息

时间:2019-02-04 19:38:56

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

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

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

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

我将不胜感激! 谢谢。

1 个答案:

答案 0 :(得分:0)

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

}

}