iProgram
iProgram

Reputation: 6547

Sending notification sound with flutter firebase_messaging plugin

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.

Upvotes: 2

Views: 10498

Answers (2)

egonzal
egonzal

Reputation: 827

Maybe it is too late. But I have struggled with notification sound in iOS. The problem was I was using Firebase to send the notification and it uses the latest API, so I had to add the "apns" part in the notification meassage:

     const payload = {
                      notification: {
                          title: "title",
                          body: "message",

                      },
                      data: {
                          info: "any data",
                          click_action: "FLUTTER_NOTIFICATION_CLICK",
                        },
                      apns:{
                        payload:{
                            aps:{
                             sound:"default"
                            }
                        }
                     },
                      condition:condition //if using topics
                 };

Upvotes: 3

Richard Heap
Richard Heap

Reputation: 51682

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);
}

Upvotes: 2

Related Questions