问题
I am trying to get my application to send a notification to a user which will alert them with their default notification sound.
So far, I am using the plugin firebase_messaging with the following code:
Message firebaseMessage = Message()
..to = token
..body = body
..title = title;
firebaseCloudMessage.send(firebaseMessage);
This allows me to send a notification to a selected user and display it on their home screen. The only problem is, it does not play a sound on iOS or give Apple Watch haptics when the notification is delivered.
How can I play a sound using the firebase_messaging framework?
If it helps, here is my configuration:
_firebaseMessaging.requestNotificationPermissions(
IosNotificationSettings(
sound: true,
badge: true,
alert: true
)
);
Sounds and haptics work if I send a message directly from firebase and enable sounds in the options, I just can't work out how to do it with this framework.
回答1:
I assume you are using the fcm_push
package. You need to add an additional tag to your message. Try:
firebaseMessage.data = [Tuple2('sound', 'default')];
This works for Android. You may need to figure out how to get fcm_push
to send the right payload for an APNS message. See the API documentation and the APNS payload reference.
(I don't use fcm_push
myself - I find it just as easy to write directly to the FCM API using HTTP. For example...)
final String url = 'https://fcm.googleapis.com/fcm/send';
Map<String, dynamic> notification = {
'body': 'some body',
'title': 'some title',
};
Map<String, dynamic> data = {
//'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'someKey': 'someValue',
'sound': 'default',
};
Map<String, dynamic> message = {
'notification': notification,
'priority': 'high',
'data': data,
'to': '', // this is optional - used to send to one device
};
Map<String, String> headers = {
'authorization': auth,
'content-type': 'application/json',
};
void sendNotification() async {
message['to'] = testToken; // todo - set the relevant values
http.Response r =
await http.post(url, headers: headers, body: json.encode(message));
print(r.statusCode);
print(r.body);
}
来源:https://stackoverflow.com/questions/54039565/sending-notification-sound-with-flutter-firebase-messaging-plugin