iOS 推送通知在颤动中不起作用

时间:2021-05-20 11:32:56

标签: ios firebase flutter firebase-cloud-messaging

我的应用需要推送通知。我只是按照 flutterfire guide 操作,android 可以正常工作,但不适用于 iOS 应用……我认为错误在于通知的配置,因为代码永远不会在 print("PUSH RECEIVED"); 或 { 中执行 FirebaseMessaging.onMessage.listen() {1}}。

我的颤振医生 -v:

FirebaseMessaging.onBackgroundMessage()

pubspec.yaml:

[√] Flutter (Channel dev, 1.27.0-4.0.pre, on Microsoft Windows [Version 10.0.19042.985], locale es-ES)
    • Flutter version 1.27.0-4.0.pre at C:\SDKs\flutter
    • Framework revision f8cd24de95 (3 months ago), 2021-02-16 11:24:17 -0800
    • Engine revision 1d537824d6
    • Dart version 2.13.0 (build 2.13.0-30.0.dev)

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at C:\Users\Windows\AppData\Local\Android\sdk
    • Platform android-30, build-tools 30.0.3
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 4.1.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] Connected device (3 available)
    • SM G986B (mobile) • ... • android-arm64  • Android 11 (API 30)
    • Chrome (web)      • chrome      • web-javascript • Google Chrome 90.0.4430.212
    • Edge (web)        • edge        • web-javascript • Microsoft Edge 90.0.818.62

• No issues found!

Main.dart:

#https://firebase.google.com/docs/flutter/setup?platform=android
firebase_core: ^1.2.0
firebase_analytics: ^8.1.0
firebase_messaging: ^10.0.0
#https://dev/packages/flutter_local_notifications/install
flutter_local_notifications: ^5.0.0+4

PodFile:

void main() async {
  ...

  await Firebase.initializeApp();
  bFirebaseMessaging.init();

  ...

  runApp(MyApp());
}

...

class _MyAppState extends State<MyApp> {

  ...

  static Future<void> _throwGetMessage(RemoteMessage message) async {
    print("PUSH RECEIVED");
    await Firebase.initializeApp();
    bFirebaseMessaging.showPushFromBackground(message);
  }

  @override
  Widget build(BuildContext context) {
    ...

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      print("PUSH RECEIVED");
      bFirebaseMessaging.showPush(message);
    });

    FirebaseMessaging.onBackgroundMessage(_throwGetMessage);

    ...
  }

  ...
}

bFirebaseMessaging.dart:

# Uncomment this line to define a global platform for your project
platform :ios, '12.0'
$FirebaseSDKVersion = '8.0.0'

...

target 'Runner' do
  use_frameworks!
  use_modular_headers!

  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
  pod 'FirebaseFirestore', :git => 'https://github.com/invertase/firestore-ios-sdk-frameworks.git', :tag => '8.0.0'
end

...

另一方面,我配置了整个 Firebase:

  • 创建了新的 iOS 应用
  • 添加了团队 ID 和应用商店 ID
  • 添加了带有密钥 ID 和团队 ID 的 APNS 身份验证密钥

额外文件:GoogleService-Info.plistSigning & Capabilities

iOS 设备收不到推送通知的原因有哪些?我想我会提供所有必要的信息,如果您需要什么,请告诉我!提前致谢!

1 个答案:

答案 0 :(得分:0)

我在 ios 中的 firebase 推送通知也有同样的问题。我的 android 推送通知工作正常,但在 ios 中却没有。

我所做的是按照firebase提供的说明集成推送通知(不是flutter指南,而是ios指南) 按照此链接 https://firebase.google.com/docs/cloud-messaging/ios/client

在 ios 中,用户必须授予权限为他们发送推送通知,这与 android 不同 所以你要做的就是将下面的代码添加到 AppDelegate.swift

if #available(iOS 10.0, *) {
  // For iOS 10 display notification (sent via APNS)
  UNUserNotificationCenter.current().delegate = self
  let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
  UNUserNotificationCenter.current().requestAuthorization(
    options: authOptions,
    completionHandler: { _, _ in }
  )
} else {
  let settings: UIUserNotificationSettings =
    UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
  application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()

希望这有效