我的 flutter 应用程序中的 Firebase 推送通知在后台时不会振动

问题描述 投票:0回答:1

Firebase 推送通知运行良好,但当应用程序在后台时,收到推送通知但手机不振动或发出声音。我已尝试了所有可能的修复方法,但没有任何方法使手机振动。下面是我的代码,我在 PushNotifications 类中初始化了 local_notification 插件。

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

  PushNotifications.init();
  PushNotifications.initLocal();
  FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    String payloadData = jsonEncode(message.data);
    if (message.notification != null) {
      String? title = message.notification?.title;
      String? body = message.notification?.body;
      if (title != null && body != null) {
        PushNotifications.showSimpleNotification(
            title: title, body: body, payload: payloadData);
      }
    }
  });
  FirebaseAnalytics.instance.setAnalyticsCollectionEnabled(true);
  runApp(const MyApp());
}

感谢您的帮助。

flutter dart push-notification firebase-cloud-messaging
1个回答
0
投票

为了确保推送通知在应用程序处于后台时触发声音和振动,您可以在通知负载中包含必要的参数。具体来说,您应该使用 Firebase 云消息传递 (FCM) 的数据负载而不是通知负载。

以下是改进代码。

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

  PushNotifications.init();
  PushNotifications.initLocal();

  // Handle background messages when the app is in the background
  FirebaseMessaging.onBackgroundMessage(_firebaseBackgroundMessage);

  // Handle foreground messages when the app is in the foreground
  FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    String payloadData = jsonEncode(message.data);

    if (message.notification != null) {
      String? title = message.notification?.title;
      String? body = message.notification?.body;

      if (title != null && body != null) {
        PushNotifications.showSimpleNotification(
          title: title,
          body: body,
          payload: payloadData,
        );
      }
    }
  });

  FirebaseAnalytics.instance.setAnalyticsCollectionEnabled(true);

  // Run the app
  runApp(const MyApp());
}

// Function to handle background messages
Future<void> _firebaseBackgroundMessage(RemoteMessage message) async {
  // Encode data payload to a string
  String payloadData = jsonEncode(message.data);

  // Check if the message does not have a notification payload but has title and body in the data payload
  if (message.notification == null &&
      message.data.containsKey("title") &&
      message.data.containsKey("body")) {
    String? title = message.data["title"];
    String? body = message.data["body"];

    // Display a simple notification if title and body are available
    if (title != null && body != null) {
      PushNotifications.showSimpleNotification(
        title: title,
        body: body,
        payload: payloadData,
      );
    }
  }
}

当在后台收到通知时,上面的代码应该执行您想要的操作。不要忘记确保发送数据有效负载。下面是在

python
中执行此操作的示例。

from firebase_admin import credentials, messaging


message = messaging.Message(
                data={"title": "title", "body": "body"},
                token="token",
            )
messaging.send(message)
© www.soinside.com 2019 - 2024. All rights reserved.