flutter:为什么手机被FirebaseMessaging锁定时不查看通知?

lkaoscv7  于 2023-02-25  发布在  Flutter
关注(0)|答案(1)|浏览(128)

我在Flutter应用程序上实现了Firebase云消息传递,它通过FirebaseMessaging插件接收json格式的消息(官方Flutter + Firebase),但当手机锁定时,我看不到通知。我还想在应用程序处于后台时实现budge。有人能给我指出正确的方向吗?
这是我的FirebaseMessaging类:

class PushNotificationSystem
{
  FirebaseMessaging messaging = FirebaseMessaging.instance;

  Future initializeCloudMessaging(BuildContext context) async
  {
    //1. Terminated
    FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage? remoteMessage)
    {
      
      if(remoteMessage != null)
      {
        //show ride request information - user information requesting ride
        readUserRideRequestInformation(remoteMessage.data["rideRequestId"], context);
      }
    });

    //2.Foreground
    FirebaseMessaging.onMessage.listen((RemoteMessage? remoteMessage)
    {
      if(remoteMessage != null)
      {
        //show ride request information - user information requesting ride
        readUserRideRequestInformation(remoteMessage!.data["rideRequestId"], context);
      }
    });

    //3. Background
    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage? remoteMessage)
    {
      if(remoteMessage != null)
      {
        //show ride request information - user information requesting ride
        readUserRideRequestInformation(remoteMessage!.data["rideRequestId"], context);
      }
    });

    FirebaseMessaging.onBackgroundMessage((RemoteMessage? remoteMessage) => readUserRideRequestInformation(remoteMessage!.data["rideRequestId"], context));
    
  }


  readUserRideRequestInformation(String userRideRequestId, BuildContext context)
  {
    FirebaseDatabase.instance.ref()
        .child("ALL Ride Requests")
        .child(userRideRequestId)
        .once()
        .then((snapData)
    {
      if(snapData.snapshot.value != null)
      {
        audioPlayer.open(Audio("music/notificationSound.mp3"));
        audioPlayer.play();

        double originLat = double.parse((snapData.snapshot.value! as Map)["origin"]["latitude"]);
        double originLng = double.parse((snapData.snapshot.value! as Map)["origin"]["longitude"]);
        String originAddress = (snapData.snapshot.value! as Map)["originAddress"];

        double destinationLat = double.parse((snapData.snapshot.value! as Map)["destination"]["latitude"]);
        double destinationLng = double.parse((snapData.snapshot.value! as Map)["destination"]["longitude"]);
        String destinationAddress = (snapData.snapshot.value! as Map)["destinationAddress"];

        String userName = (snapData.snapshot.value! as Map)["userName"];
        String userPhone = (snapData.snapshot.value! as Map)["userPhone"];

        String? rideRequestId = snapData.snapshot.key;

        //Pass everything to the MODEL class UserRideRequestInformationc
        UserRideRequestInformation userRideRequestDetails = UserRideRequestInformation();

        userRideRequestDetails.originLatLng = LatLng(originLat, originLng);
        userRideRequestDetails.originAddress = originAddress;

        userRideRequestDetails.destinationLatLng = LatLng(destinationLat, destinationLng);
        userRideRequestDetails.destinationAddress = destinationAddress;

        userRideRequestDetails.userName = userName;
        userRideRequestDetails.userPhone = userPhone;

        userRideRequestDetails.rideRequestId = rideRequestId;

        showDialog(
            context: context,
            barrierDismissible: false,
            builder: (BuildContext context) => NotificationDialogBox(
              userRideRequestDetails: userRideRequestDetails,
            ),
        );
      }
      else
      {
        Fluttertoast.showToast(msg: "This Ride Request Id do not exists. ");
      }
    });

  }

  Future generateAndGetToken() async
  {
    
    String? registrationToken = await messaging.getToken();
    print("FCM Registration Token: ");
    print(registrationToken);

    //lo setto nel databese realtime
    FirebaseDatabase.instance.ref()
        .child("drivers")
        .child(currentFirebaseUser!.uid)
        .child("token")
        .set(registrationToken);

    messaging.subscribeToTopic("allDrivers");
    messaging.subscribeToTopic("allUsers");
  }

请帮帮我

jm81lzqq

jm81lzqq1#

尝试将这些行添加到manifest.xml中

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.BACKGROUND_SERVICE"/>

如果不起作用,请搜索如何在后台运行应用程序时运行Firebase通知。

相关问题