Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase push notification with custom sound (Flutter)

Tags:

flutter

dart

I'm currently using firebase messaging cloud to push notification for my app. I'm trying to make a custom notification sound for the push notification. I believe that it can be done by putting "sound: blabla.mp3" inside the payload, but how do i define the sound inside dart page?

like image 716
ali Avatar asked Nov 29 '18 03:11

ali


People also ask

How do you implement Firebase push notifications in Flutter?

To send a notification, go to Firebase Console → Cloud Messaging and click on Send your first message. Then enter the Title and body field. If you wish to send it to a particular device then click on Send test message and enter the FCM registration token. (Enter the current FCM registration token).

How do I make custom push notifications?

1. Configure custom push notifications in the Dashboard. You can configure Helpshift to send the push notification message to a custom URL instead of through APNS (For iOS) and FCM/GCM (for Android). The first step to do so is for an Admin to set up a custom push notification.


2 Answers

It can be done flutter_local_notifications plugin.

First, you should define channels for both Android and iOS:

const androidPlatformChannel = AndroidNotificationDetails(
  'ANDROID_CHANNEL_ID',
  'Name',
  'Description',
  color: Color.fromARGB(255, 0, 0, 0),
  importance: Importance.max,
  sound: RawResourceAndroidNotificationSound('notification_sound'),
  playSound: true,
  priority: Priority.high,
  showWhen: false,
);

const iOSPlatformChannel = IOSNotificationDetails(
  sound: 'notification_sound.aiff',
  presentAlert: true,
  presentBadge: true,
  presentSound: true,
);

const platformChannel = NotificationDetails(
  android: androidPlatformChannel,
  iOS: iOSPlatformChannel,
);

Then show a notification:

await flutterLocalNotificationsPlugin.show(
      id,
      title,
      body,
      platformChannel,
      payload: notification.payload,
    );

IMPORTANT! notification_sound.aiff file should be copied with XCode

like image 175
K.Amanov Avatar answered Sep 20 '22 12:09

K.Amanov


Use flutter_local_notifications package

AndroidNotificationDetails androidNotificationsDetails = AndroidNotificationDetails(
       'your other channel id',
       'your other channel name',
       'your other channel description',
        importance: Importance.Max,
        priority: Priority.Max,
        enableLights: true,
        playSound: true,
        sound: RawResourceAndroidNotificationSound('notification'),
);

Note: you should add notification.mp3 file in android/app/src/main/res/raw/notification.mp3 and don't forget to specify playSound:

playSound: true,

this worked for me in the foreground / background and when the app is closed.

like image 25
Abdelouahed Medjoudja Avatar answered Sep 16 '22 12:09

Abdelouahed Medjoudja