1

I'm building a chat app and using FCM for notification. Whenever a user receives a new message, he gets a pop-up notification when the app is in the foreground.

However, I would like to customize the notification such that it will not show the pop-up message but only vibrate when the user is on the chat screen itself. When he is not on the chat screen, the notification will show the pop-up and vibrate.

Any idea how to go about doing this client-side? Thank you in advance!

scott lee
  • 546
  • 5
  • 23

1 Answers1

0

There are two types of notifications, data message and notification message. Notification messages are not handled by your app, but you have control on data messages.

Secondly, you need to check whether you are on chat screen with a specific user who sent you a message or not. To fulfil this requirement, you can save the userID at any place (SharedPref, Utils or any singleton) when you open chat room and remove it when you leave chatRoom. Now when you receive notification, just compare it with your saved userID to whether you want to show notification or vibrate

For only vibrating your phone, you can use package flutter_vibrate 1.0.0 and for generating notification you can use package flutter_local_notifications

FirebaseMessaging.onMessage.listen((RemoteMessage message) { 
if(message.data.senderID == prefs.getString(‘chatRoomID)){ 

   // Check if the device can vibrate
  bool canVibrate = await Vibrate.canVibrate;

 // Vibrate
 // Vibration duration is a constant 500ms because
 // it cannot be set to a specific duration on iOS.
 Vibrate.vibrate();

 // Vibrate with pauses between each vibration
final Iterable<Duration> pauses = [
const Duration(milliseconds: 500),
const Duration(milliseconds: 1000),
const Duration(milliseconds: 500),
];
// vibrate - sleep 0.5s - vibrate - sleep 1s - vibrate - sleep 0.5s - vibrate
Vibrate.vibrateWithPauses(pauses);
 } Else{  AndroidNotificationDetails(
    'your channel id', 'your channel name', 'your channel description',
    importance: Importance.max,
    priority: Priority.high,
    showWhen: false); } 
}); 

One more thing which you also need to do is to show that new message in chat screen. For this, you can use Stream Builder.

bilal
  • 2,187
  • 3
  • 22
  • 23
  • Thanks! So you are saying, I can input the userID of the sender in the data messages and compare it to the userID I stored in the SharedPref. Seems like a good idea. Now I am wondering how do you control whether you want the notification to vibrate or show the notification message? – scott lee Jun 24 '21 at 06:20
  • I have written some rough code below. Can you take a look at the commented part and let me know how you would go about doing it? FirebaseMessaging.onMessage.listen((RemoteMessage message) { if(message.data.senderID == prefs.getString(‘chatRoomID)){ //only vibrate <- but how do you make the notification only vibrate? What code do you put here to get this function? } Else{ //vibrate +sound + show notification message<- what code do you put here? } }); Thank you for your help! – scott lee Jun 24 '21 at 06:20
  • 1
    @scottlee Edited my answer, but this is not actual code which will run. It gives you an idea for how to do this. – bilal Jun 24 '21 at 06:35
  • Thanks for your prompt reply. I'll test it out and see how it works. Will revert shortly :) – scott lee Jun 24 '21 at 07:29
  • do you know why the notification is still showing and there is still vibration even when I empty the onMessage code. Below are some snippets of my code: – scott lee Jun 25 '21 at 03:45
  • await FirebaseMessaging.instance .setForegroundNotificationPresentationOptions( alert: true, badge: true, sound: true, ); } – scott lee Jun 25 '21 at 03:45
  • FirebaseMessaging.onMessage.listen((RemoteMessage message) async { }); – scott lee Jun 25 '21 at 03:45
  • I am merely listening to the onMessage and not writing any functions upon receiving the notification. However, when I send the notification after sending it from Firebase console, there is still a pop-up notification, with vibration and sound. – scott lee Jun 25 '21 at 03:48