Sending notification sound with flutter firebase_messaging plugin

点点圈 提交于 2020-05-16 16:13:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!