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

sgtfey8w  于 12个月前  发布在  Flutter
关注(0)|答案(1)|浏览(153)

Firebase推送通知工作正常,但当应用程序在后台时,收到推送通知,但手机不振动或发出声音。我已经尝试了所有可能的修复方法,但没有任何方法使手机振动。下面是我的代码,我在Pushbutton类中初始化了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());
}

字符串
谢谢你的帮助

zvokhttg

zvokhttg1#

为了确保推送通知在应用处于后台时触发声音和振动,您可以在通知负载中包含必要的参数。具体而言,您应该使用Firebase Cloud Messaging(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)

相关问题