【问题标题】:iOS push notifications not working in flutteriOS推送通知无法正常工作
【发布时间】:2021-08-09 15:46:05
【问题描述】:

我的应用需要推送通知。我只关注flutterfire guide,android 正常工作,但不适用于 iOS 应用程序...我认为错误出在通知配置中,因为代码永远不会在 FirebaseMessaging.onMessage.listen()FirebaseMessaging.onBackgroundMessage() 内执行 print("PUSH RECEIVED");

我的颤振医生-v:

[√] 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!

pubspec.yaml:

#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

Main.dart:

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);

    ...
  }

  ...
}

Pod 文件:

# 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

...

bFirebaseMessaging.dart:

class bFirebaseMessaging {

  ...

  static Future init() async {

    // Declaration of variables
    FirebaseMessaging firebaseMessaging = FirebaseMessaging.instance;
    NotificationSettings settings = await firebaseMessaging.requestPermission(
      alert: true,
      announcement: false,
      badge: true,
      carPlay: false,
      criticalAlert: false,
      provisional: false,
      sound: true,
    );

    print('User granted permission: ${settings.authorizationStatus}');

    if(Platform.isIOS){
      await firebaseMessaging.setForegroundNotificationPresentationOptions(
        alert: true, // Required to display a heads up notification
        badge: true,
        sound: true,
      );
    }
  }

  ...

}

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

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

额外文件:

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

【问题讨论】:

    标签: ios firebase flutter firebase-cloud-messaging


    【解决方案1】:

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

    我所做的是按照 firebase 提供的指令集成推送通知(不是颤振指南,而是 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()
    

    希望这可行

    【讨论】:

    • 有用的链接,在文档中没有找到
    猜你喜欢
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-16
    相关资源
    最近更新 更多