首页 > 解决方案 > Flutter firebase消息:iOS手机没有消息捕获

问题描述

我正在尝试在我的应用程序上使用颤振 firebase 消息传递。我在pubspec.yaml文件中安装了这些库:

dependencies:
    ...
    firebase_core: ^0.5.1
    firebase_messaging: ^8.0.0-dev.9

在 android 中我没有任何问题但在 Ios 中,即使我的应用程序在前台,我也没有收到任何消息。事实上FirebaseMessaging.onMessageFirebaseMessaging.onBackgroundMessage没有触发。

Future initialise() async {
    //Firebase.initializeApp() I initialized on main method first
    FirebaseMessaging _fcm = FirebaseMessaging.instance;

    _fcm.requestPermission(alert: true, badge: true, sound: true);
    if (Platform.isAndroid) {
      _token = await _fcm.getToken();
      print("------> token is: $_token");
      } else if (Platform.isIOS) {
        _token = await _fcm.getAPNSToken(); // token received from firebase
        print("------> token is: $_token");

    }

    FirebaseMessaging.onMessage.listen((remoteMessage) async {
      print('[onMessage] message: ${remoteMessage.data}');
      var msg = PushMsgModel().wrapOriginMessage(remoteMessage);
      _showOverlyNotify(msg);
    });

    FirebaseMessaging.onBackgroundMessage(onBackgroundMessage);
  }

在此处输入图像描述

AppDelegate.swift 包括:

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    if #available(iOS 10.0, *) {
      UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
    }
    
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

但是当我从firebase控制台推送消息时,我无法在android studio控制台上收到任何消息?我的错误是什么?有人能帮我吗?

当应用程序可以获取令牌意味着我的配置是正确的?

我正在使用软件版本 14.4在iPhone7plus(mobile)上进行测试

标签: flutterfirebase-cloud-messagingflutter-ios

解决方案


我在真实设备上遇到了与 IOS 15.2 相同的问题,FirebaseMessaging.onBackgroundMessage而我对 Android 没有任何问题。我花了几天时间测试不同的建议,三重检查我的配置,更新依赖项,测试自定义 APN 有效负载。没有任何效果。

对于我的情况,我意识到每次flutter run在我的测试之间运行时,应用程序在调试模式下运行都会接受一个新的UUID,因为会发生应用程序重新安装,因此,应用程序无法映射当应用程序具有新 UUID 时到达的通知并且从未触发后台处理程序。

当我在真实设备上以发布模式flutter run --release对其进行测试时,确保UUID保持不变()最终我得到了预期的结果。iosDeviceInfo.identifierForVendor


推荐阅读