Flutter Android Local Notification

Created At: 2023-02-09 18:04:49 Updated At: 2023-02-13 04:15:54

In this tutorial I will cover Android and iOS Local Notification set up and different issues like playing sound, customize sound and registering multiple channels. I decided to do share my knowledge which I have worked on the project Flutter audio and video call app.

First let's take a look at some of the settings properties.

Channel Id

This is the first argument. Give it any unique string. 

Channel name

This is a string too. It should also be unique. If you used the channel 

in other places make sure they are the same.  Like if you do it during Facebook login, you need to set up a channel name too.

If you have server side code, you need to have the same channel name everywhere.

 

Channel description

This one could be anything about it. You may also skip this. It’s string type

Channel groupId

The id of the group that the channel belongs to. It’s skippable as well.

Importance

The importance should be set based on your channel type. If you channel type messaging or calling, importance should be set to high. If it’s sending message to topic or to a large number of groups then it could be set to other values.

Play sound 

This is a bool type. The default value is set to true. It means sound will always play unless you specify differently. Indicates if a sound should be played when the notification is displayed. Tied to the specified channel and cannot be changed after the channel has been created for the first time. That means if you change the channel name during one build it won’t work. Delete the build and change the channel name and rebuild your apk. 

Sound

The sound to play for the notification. Requires setting [playSound] to true for it to work. If [playSound] is set to true but this is not specified then the default sound is played. Tied to the specified channel and cannot be changed after the channel has been created for the first time.

Enable vibration

It’s bool and it Indicates if vibration should be enabled when the notification is displayed. Tied to the specified channel and cannot be changed after the channel has been created for the first time.

Vibration pattern

It’s bool and it Indicates if lights should be enabled when the notification is displayed. Tied to the specified channel and cannot be changed after the channel has been created for the first time.

Show badge 

Whether notifications posted to this channel can appear as application icon badges in a Launcher

Multiple channels and sounds

In your entry point of app have the code.

  if(GetPlatform.isAndroid){
    FirebaseMessagingHandler.flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()!
        .createNotificationChannel(FirebaseMessagingHandler.channel_call);
    FirebaseMessagingHandler.flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()!
        .createNotificationChannel(FirebaseMessagingHandler.channel_message);
  }

This is help to register channels. Here we are registering two channels. One for voice call and another one is text message. They should be different sounds, because for voice call you will play long sound and for text message you will play short sound.

Make sure that, you have flutter local notification plugin installed. It won't work without that.

In your notification class, create instances of multiple channels.

  static AndroidNotificationChannel channel_call = const AndroidNotificationChannel(
    'com.dbestech.chatty_chat.call', // id
    'chatty_call', // title
    importance: Importance.max,
    enableLights: true,
    playSound: true,
    sound: RawResourceAndroidNotificationSound('alert'),
    enableVibration: true
  );
  static AndroidNotificationChannel channel_message = const AndroidNotificationChannel(
    'com.dbestech.chatty_chat.message', // id
    'chatty_message', // title
    importance: Importance.defaultImportance,
    enableLights: true,
    playSound: true,
   // sound: RawResourceAndroidNotificationSound('alert'),
  );

First one is for voice call, with this we are playing a customized sound. For the second one, we are playing a default android sound.

Comment

Add Reviews