2

I am trying to play a custom sound when a push notification is received on my flutter app. However it only works when app is opened. How do I ensure users get to hear the sound when the app is closed or in background.

class PushNotificationService{
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  Future initialize(context) async{
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        fetchRideInfo(message['data']['orderId'], context);
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
        fetchRideInfo(message['data']['orderId'], context);
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
        fetchRideInfo(message['data']['orderId'], context);
      },
    );
  }
  Future<String> getToken() async{
    print("fetching token");
    token = await _firebaseMessaging.getToken();
    print('token got as: $token');

  }

  void fetchRideInfo(orderId, context) {
    print("fetching info");
    showDialog(
      barrierDismissible: false,
      context: context,
      builder: (BuildContext context) =>
          CustomProgressDialog(status:'Fetching details',),);
      assetsAudioPlayer.open(
      Audio('sounds/alert.mp3'),
    );
    assetsAudioPlayer.play();
  }




}

Notification Body

{
    "notification": {
    "body": "this is a body",
    "title": "this is a title"
     }, 
    "data": {
    "click_action":"FLUTTER_NOTIFICATION_CLICK", 
    "type": "normal",
    "id": "1", 
    "ride_id": "-MKp1WBP8G2kTgJBqwyl",
    "status": "done"},
    "to":"dv9Yq4qHSVubIohoM8niDX:APA91bGJgQkP7Ir867vyo9oT1CeXbWPKP6xMSDUgwu3242W9odgr4vn5qIIgPmY2LQyPNjL8Pwl8GcRY4TbucxVcCBOM0eBZDHnfhh0E0qYvhdWc_0Yd1oBT_Vh1sA0q-haqXQ3KXib5"

     }
custom apps
  • 413
  • 1
  • 8
  • 19

0 Answers0