How set custom sound notification on background in flutter?

15,892

You can write background handler method for firebase messaging, then you can call showNotification method in background handler. Example code:

Future<dynamic> onBackgroundMessageHandler(Map<String, dynamic> message) async {

  if (message['data'] != null) {
    final data = message['data'];
    final title = data['title'];
    final body = data['message'];
    showNotification(title, body);
  } 

  return Future<void>.value();
}


FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

_firebaseMessaging.configure(onBackgroundMessage: Platform.isIOS ? null : onBackgroundMessageHandler);
Share:
15,892

Related videos on Youtube

korosiadam99
Author by

korosiadam99

Updated on May 25, 2022

Comments

  • korosiadam99
    korosiadam99 almost 2 years

    I wrote a flutter application and I can't configure the firebase cloud messaging with custom sound. I get notifications, but they come with the default sound while the app is in background. In the foreground, I use the local notification library and it works well, but I need to work in background too.

    This is what I send for the cloud messaging:

    {
       "to":"<firebase_token>",
       "notification":{
          "sound":"arrive",
          "title":"My Title",
          "body":"My body"
       },
       "data":{
          "click_action":"FLUTTER_NOTIFICATION_CLICK",
          "status":"done",
          "screen":"screenA",
          "message":"ACTION"
       },
       "apns":{
          "headers":{
             "apns-priority":"5",
             "apns-push-type":"background"
          },
          "payload":{
             "aps":{
                "content-available":1
             }
          }
       }
    }
    

    This is my working local notification config:

    void showNotification({
        String title,
        String body,
      }) {
        var androidPlatformChannelSpecifics = AndroidNotificationDetails(
          'your channel id',
          'your channel name',
          'your channel description',
          importance: Importance.Max,
          priority: Priority.Max,
          ticker: 'ticker',
          playSound: true,
          sound: RawResourceAndroidNotificationSound('arrive')
        );
    
        var iOSPlatformChannelSpecifics = IOSNotificationDetails();
    
        var platformChannelSpecifics = NotificationDetails(
          androidPlatformChannelSpecifics,
          iOSPlatformChannelSpecifics,
        );
        notifications.show(0, title, body, platformChannelSpecifics,
            payload: 'Custom_Sound',);
      }
    

    So the local notifications library see my custom sound, but cloud messaging will play the default sound. What could be the problem?

    My sound is located at: android\app\src\main\res\raw\arrive.mp3

    My imports are:

        flutter_local_notifications: ^1.4.3 
        firebase_messaging: ^6.0.16
    

    Flutter doctor:

    [√] Flutter (Channel stable, v1.12.13+hotfix.9, on Microsoft Windows [Version 10.0.18362.836], locale hu-HU)
        • Flutter version 1.12.13+hotfix.9 at C:\flutter src\flutter
        • Framework revision f139b11009 (8 weeks ago), 2020-03-30 13:57:30 -0700
        • Engine revision af51afceb8
        • Dart version 2.7.2
    
     
    [√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
        • Android SDK at C:\Users\koros\AppData\Local\Android\sdk
        • Android NDK location not configured (optional; useful for native profiling support)
        • Platform android-29, build-tools 28.0.3
        • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
        • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
        • All Android licenses accepted.
    
    [√] Android Studio (version 3.4)
        • Android Studio at C:\Program Files\Android\Android Studio
        • Flutter plugin version 35.3.1
        • Dart plugin version 183.6270
        • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
    
    [√] VS Code (version 1.45.1)
        • VS Code at C:\Users\koros\AppData\Local\Programs\Microsoft VS Code
        • Flutter extension version 3.10.2
    
    [√] Connected device (1 available)
        • SM A520F • 52003aa8f4ea64d5 • android-arm64 • Android 8.0.0 (API 26) (emulator)
    
    • No issues found!
    
  • korosiadam99
    korosiadam99 almost 4 years
    Thank's, I try it, but doesent work for me. The local notification library could't wake up in that onBackgroundMessageHandler :/ That lines works in the "onMessage" parameter, but here not. What do you think, Is it normal or something wrong with my local notifications too?
  • Adem
    Adem almost 4 years
    Firstly, you need to write onBackgroundMessageHandler method at the top of the main.dart file then you need to write notification method(local notification) to other dart file (you can create class in other dart file). Because you can't access to showNotification method from top of the file. After then you can call your showNotification method. Example Code: paste.ubuntu.com/p/HPRTkprkwX
  • korosiadam99
    korosiadam99 almost 4 years
    Works, thank you! Firstly i get an exeption "PluginRegistrantCallback is not set", i did some mistake in my android folder. I corrected, and it starts to work. You helped me a lot, thank's for it! :)
  • Amir Aslam
    Amir Aslam over 3 years
    possible to stop arrive.mp3 sound in the middle using local notification library? I mean stop the sound while it is playing when the notification in received
  • Nicks
    Nicks over 3 years
    Any idea about ios?
  • Xuala khawlhring
    Xuala khawlhring over 2 years
    After following this, I was able to show notification with custom sound but there are 2 notifications on the tray, the one from firebase messaging and another from local_notification ...I called localNotification.show message inside the on backgroundMessage of firebase messaging. How do I not show the firebase Messaging notification?
  • Sharath B Naik
    Sharath B Naik over 2 years
    @korosiadam99 I'm also getting PluginRegistrantCallback error. What you did in android folder.