当我的 flutter 应用程序收到推送通知时,我正在尝试播放自定义声音。但是,它仅在打开应用程序时有效。我如何确保用户在应用关闭或在后台听到声音。
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": "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"
}