【发布时间】: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.onMessage 和FirebaseMessaging.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