【问题标题】:Flutter firebase message: No message catch by iOS phoneFlutter firebase消息:iOS手机没有消息捕获
【发布时间】:2021-05-31 20:05:01
【问题描述】:

我正在尝试在我的应用程序上使用颤振 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);
  }
  • 我添加的IOS配置

    <key>FirebaseAppDelegateProxyEnabled</key>
    <false/>
    

    Info.plist.

  • 我创建了 APNs 证书并添加到 ios firebase 项目设置中

  • 我注册了一个应用标识符

  • 我生成了一个配置文件,并在 xcode 中添加到 Provisioning Profile

  • 在 Xcode 中,我在 Project Navigator 中选择了 Runner。在

  • 功能选项卡我打开了推送通知和后台模式, 并在后台模式下启用后台获取和远程通知。

  • 我还复制了 GoogleService-Info.plist 文件,在 ios/Runner.xcworkspace 与 Xcode。

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 控制台上收到任何消息? 我的错误是什么?有人可以帮我吗?

应用什么时候能拿到token就说明我的配置是对的?

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

【问题讨论】:

  • 在 AppDelegate.swift 中添加 import Firebase & FirebaseApp.configure()
  • 我必须添加FirebaseApp.configure(!在if #available(iOS 10.0, *) { 之前? @Priyesh
  • 是的,就在if #available(iOS 10.0, *) {线上方
  • 什么也没发生。打印 onMessage.listen 没有打印。@Priyesh

标签: flutter firebase-cloud-messaging flutter-ios


【解决方案1】:

更新 iOS 前台通知显示选项以允许在您的 runApp() 之前进行提醒通知。

await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
    alert: false,
    badge: false,
    sound: false,
  );

【讨论】:

  • FirebaseMessaging.onMessage.listen 不会调用 bro?setForegroundNotificationPresentationOptions 在 onMessage 之前调用? @Rahul
  • 不像我想象的那样工作。因为我的消息似乎没有到达应用程序并且onMessage.listen 根本没有触发@Rahul
  • 你可以尝试删除这个吗? if #available(iOS 10.0, *) { UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate }
  • 另外,你可以更新包试试吗? ^8.0.0-dev.11 或更高版本
  • 我更新到 ^8.0.0-dev.14 但我也有理智的问题@Rahul
猜你喜欢
  • 2017-09-18
  • 2020-10-19
  • 2021-07-09
  • 2018-09-07
  • 2021-04-18
  • 1970-01-01
  • 2017-06-03
  • 1970-01-01
  • 2021-02-19
相关资源
最近更新 更多