How to handle fcm when app in in background and play sound on notification arrival in flutter - firebase

I want to play a sound when a notification arrives, but I am not able to play. If the app is opened the sound is played and is working fine. But when the app is killed I see the notification but the sound is not played.
Please help how to do it.
#override
void initState()
{
super.initState();
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print('on message $message');
String key = message["data"]["fcm_call_api"];
if (key == "approval") {}
if(Platform.isAndroid)``
{
_playSound();
}
},
onResume: (Map<String, dynamic> message) async {
},
onLaunch: (Map<String, dynamic> message) async {
_playSound();
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
});
_firebaseMessaging.getToken().then((String token) {
assert(token != null);
setState(() {
_homeScreenText = "Push Messaging token: $token";
});
});
}
void _playSound() {
AudioCache player = new AudioCache();
const alarmAudioPath = "notification_tone.mp3";
player.play(alarmAudioPath);
}

You did not specify on which platform you experience this problem, but overall background messages with the firebase messaging plugin is very limited right now, but there is a PR actively fixing this: https://github.com/flutter/plugins/pull/1900 (related issue: https://github.com/flutter/flutter/issues/22072)
Right now it is documented behavior that messages arriving in the background are only delivered once it is brought back into foreground (on android). See it's documentation at https://github.com/flutter/plugins/tree/master/packages/firebase_messaging#receiving-messages:

Related

Firebase messaging - Notifications are not sent when app is Closed or sleep in Flutter

I have built my app in flutter and I have implemented both LocalNotifications and FCM messaging.
this is my code:
final FirebaseMessaging firebaseMessaging = FirebaseMessaging();
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
#override
void initState() {
super.initState();
registerNotification();
configLocalNotification();
}
void registerNotification() {
firebaseMessaging.requestNotificationPermissions();
firebaseMessaging.configure(onMessage: (Map<String, dynamic> message) {
print('onMessage: $message');
return ;
}, onResume: (Map<String, dynamic> message) {
print('onResume: $message');
return Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NotificationsScreen()));
},
onLaunch: (Map<String, dynamic> message) {
print('onLaunch: $message');
return;
});
firebaseMessaging.getToken().then((token) {
print('token: $token');
FirebaseFirestore.instance
.collection('Consultant')
.doc(firebaseUser.uid)
.update({'deviceToken': token});
}).catchError((err) {
//Fluttertoast.showToast(msg: err.message.toString());
});
}
Future selectNotification(String payload) async {
if (payload != null) {
debugPrint('notification payload: $payload');
}
await Navigator.push(
context,
MaterialPageRoute<void>(builder: (context) => NotificationsScreen(payload: payload,)),
);
}
void showNotification(message) async {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
Platform.isAndroid
? 'it.wytex.vibeland_pro_app'
: 'it.wytex.vibeland_pro_app',
'Vibeland Pro',
'Vibeland Pro',
playSound: true,
enableVibration: true,
importance: Importance.max,
priority: Priority.high,
);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
android: androidPlatformChannelSpecifics, iOS: iOSPlatformChannelSpecifics);
print(message);
print(message['body'].toString());
print(json.encode(message));
await flutterLocalNotificationsPlugin.show(0, message['title'].toString(),
message['body'].toString(), platformChannelSpecifics,
payload: json.encode(message));
await flutterLocalNotificationsPlugin.show(
1, '📩 Hai ricevuto un messaggio 📩 ', 'Controlla subito le Tue notifiche 🔔🔔', platformChannelSpecifics,
payload: 'item x',
);
}
void configLocalNotification() {
var initializationSettingsAndroid =
new AndroidInitializationSettings('#mipmap/ic_launcher');
var initializationSettingsIOS = new IOSInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: true,
);
var initializationSettings = new InitializationSettings(
android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings);
}
I have built a function in firebase to push some New collections as notifications.
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
const fcm = admin.messaging();
exports.sendNotification = functions.firestore
.document("Notifications/{id}")
.onCreate((snapshot) => {
const name = snapshot.get("name");
const subject = snapshot.get("subject");
const token = snapshot.get("token");
const payload = {
notification: {
title: "" + name,
body: "" + subject,
sound: "default",
click_action: "FLUTTER_NOTIFICATION_CLICK",
},
};
return fcm.sendToDevice(token, payload);
});
the version of firebase_messaging: ^7.0.3 and flutter_local_notifications: ^4.0.1
at the moment I don't upgrade due to some conflict with dependencies.
In this way I got both notifications when an app is open I get Local notifications correctly and when an app is in foreground and background I get Firebasemessaging according to the new collection added into my firestore.
The problem now comes when I close the app or the app after some minutes starts to sleep...
I can't get any notifications
To start again to get notifications, I need to run again the app and wake the app.
This is a problem with my app because my Apps notifications are very important and users need to get them always.
As you can on the FlutterFire documentation, foreground and background notification are handled differently by the plugin, so there are 2 thing you need to fix in your app.
First you need to prepare your Cloud Function to send background notifications as well as foreground, and in order to to that, you need to prepare your json to not only have a notification but also a data field, as follows:
const payload = {
notification: {
title: "" + name,
body: "" + subject,
sound: "default",
},
data: {
click_action: "FLUTTER_NOTIFICATION_CLICK"
}
};
Second, you are going to need configure your firebaseMassaging to receive background messages, like this:
firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) {
print('onMessage: $message');
return ;
},
onResume: (Map<String, dynamic> message) {
print('onResume: $message');
return Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NotificationsScreen()));
},
onLaunch: (Map<String, dynamic> message) {
print('onLaunch: $message');
return;
},
onBackgroundMessage: myBackgroundMessageHandler
);
And finally you need to create a handler that will manually handle background messages, following the example in the documentation you can do something like this:
Future<void> myBackgroundMessageHandler(RemoteMessage message) async {
print("Handling a background message: ${message}");
}
Adding the Line:
onBackgroundMessage: myBackgroundMessageHandler
Future<void> myBackgroundMessageHandler(RemoteMessage message) async {
print("Handling a background message: ${message}");
}
I got these error:
Nexus, [10.03.21 16:59]
[ File : app-release.apk ]
Amore, [10.03.21 17:09]
java.lang.RuntimeException: Unable to create service io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService: java.lang.RuntimeException: PluginRegistrantCallback is not set.
at android.app.ActivityThread.handleCreateService(ActivityThread.java:4023)
at android.app.ActivityThread.access$1600(ActivityThread.java:224)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1903)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7562)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.RuntimeException: PluginRegistrantCallback is not set.
at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.C(Unknown Source:70)
at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.onCreate(Unknown Source:40)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:4011)
... 8 more

Is it possible to implement notification grouping/bundling using FCM in Flutter?

I try to implement group notification like this android group notification and ios group notification. But I can't able to do it. I tried this flutter_local_notification plugin too. but this works only when app open. not working on foreground(onResume) and background.
void registerNotification() {
_fcm.configure(
onMessage: (Map<String, dynamic> message) {
return;
},
onResume: (Map<String, dynamic> message) {
return;
},
onLaunch: (Map<String, dynamic> message) {
return;
},
onBackgroundMessage: backgroundMessageHandler);
}
payload
const payload = {
notification: {
title: title,
body: message,
},
data: {
click_action: "FLUTTER_NOTIFICATION_CLICK",
sound: "default"
},
android: {
priority: "high",
collapse_key: userName,//tried to add collapse_key for group notification
},
apns: {
headers: {
"apns-priority": "5",
},
},
token:token,
};
SOLUTION
I saw the react-native answer for this, you have to do same thing using Flutter with firebase_messaging react native answer
If you want to submit a group, you need to subscribe to a topic, in the documentation that mentions it, you would no longer send a single token, but would instead subscribe to a topic and submit it to that topic.

Flutter fcm redirect issue

Hi in one of my flutter project, I am using firebase messaging. First a splash screen and second, the main page of the application. In second page, I implemented the firebase.configure method in the init state as follows. The _navigateToItemDetail method leads to an another page
#override
void initState() {
super.initState();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
setState(() {
_newNotification = true;
});
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
_navigateToItemDetail(message);
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
_navigateToItemDetail(message);
},
);
and I cam calling the web service for this page after this. But the above method will launch after the webservice calls. So that will cause error in the page redirection. I just put a delay of 4 seconds in web service call, then it will works fine. Is there is any method to solve the issue ? async method available for firebase config ?
I think you need 'onBackgroundMessage'
firebaseMessaging.configure(
//...
onBackgroundMessage: Platform.isIOS ? null : myBackgroundMessageHandler
//...
)
//...
static Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
//Do here something
}
as you can see, it only work on android

Converting Firebase Token to String using Flutter

I have an application that is supposed to send the Firebase Token from my Flutter app to an ASP.Net App Server. The endpoint on the app server works - the request from the Flutter app to the App Server is not working.
The reason it is not working is because when I try to send the token, the token doesn't appear to have arrived yet - it's of type Future. How do I turn that token into a string when it finally arrives?
I've tried turning the token directly into a string in the fcmStream.Listen function, I've also tried turning it into a string using _firebaseMessaging.getToken. Neither of them work
FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
#override
void initState() {
// TODO: implement initState
super.initState();
location.onLocationChanged().listen((value) {
if (this.mounted) {
setState(() {
currentLocation = value;
});
}
});
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) {
print('on message $message');
},
onResume: (Map<String, dynamic> message) {
print('on resume $message');
},
onLaunch: (Map<String, dynamic> message) {
print('on launch $message');
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
String clientToken = _firebaseMessaging.getToken().then((token) {
print("Token Init: " + token.toString());
}
).toString();
BackendService.postToken(clientToken.toString(), "email#gmail.com");
#override
Stream<String> fcmStream = _firebaseMessaging.onTokenRefresh;
fcmStream.listen((token) {
/*print("Token On Refresh: " + token);
BackendService.postToken(token.toString(), "email#gmail.com");*/
}
);
fcmStream.toString();
class BackendService {
static Future<String> postToken(String token, String hostEmail) async {
final responseBody = (await http.get(
'realurlomitted/.../Meets/RegisterDevice?token=$token&hostEmail=$hostEmail')).body;
print(" Response: " + responseBody.toString());
return responseBody.toString();
}
}
Whenever the token.toString prints, it prints the token just fine - I can see that. It just seems like whenever it tries to make the post using http, the token hasn't arrived from whatever getToken is.
If I can turn that Futrure into a string by awaiting it or something, it would solve my problem so that the $token parameter is the token as a string.
More specifically, my request URL should look like:
https://-----/Meets/RegisterDevice?token=c6V49umapn0:Jdsf90832094890s324&hostEmail=email#gmail.com
But it looks like:
https://-----/Meets/RegisterDevice?token=instance of Future<Dynamic>&hostEmail=email#gmail.com
In the Flutter debugger
As you said, awaiting the future will solve your problem. You can write an async function and put the code in your initState inside it and use await, or you can do this:
_firebaseMessaging.getToken().then((token) {
final tokenStr = token.toString();
// do whatever you want with the token here
}
);
This style is now available
FirebaseAuth.instance.currentUser().then((user) {
if (user != null) {
user.getIdToken().then((token) {
Map<dynamic,dynamic> tokenMap = token.claims;
print(tokenMap['sub']);
});
}
});
so this complete code
#override
void initState() {
FirebaseAuth.instance.currentUser().then((user) {
if (user != null) {
user.getIdToken().then((token) {
Map<dynamic,dynamic> tokenMap = token.claims;
print(tokenMap['sub']);
});
}
});
super.initState();
}

Firebase clod messaging in flutter : open specific route when clicking on a notification (get a context)

I'm using firebase_messaging to get push notifications on the app .. I'm sending with the notification a route to display a specific page when clicking on the notification with the help of flutter_local_notification like this:
(I've not a good context)
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) {
print('on message $message');
print(message['route']);
globals.firebaseIn(message['route']);
},
onResume: (Map<String, dynamic> message) {
print('on resume $message');
globals.firebaseOut(message['route']);
},
onLaunch: (Map<String, dynamic> message) {
print('on launch $message');
globals.firebaseOut(message['route']);
},
);
void firebaseIn(String route) {
showNotificationWithDefaultSound(route);
}
Future onSelectNotification(String payload) async {
router.navigateTo(currentContext, payload,
transition: TransitionType.inFromRight,
transitionDuration: const Duration(milliseconds: 500));
}
Future showNotificationWithDefaultSound(String route) async {
var androidPlatformChannelSpecifics = new
AndroidNotificationDetails(
'your channel id', 'your channel name', 'your channel description',
importance: Importance.Max, priority: Priority.High);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'استطلاع جديد',
'لديك استطلاع جديد',
platformChannelSpecifics,
payload: route,
);
}
void firebaseOut(String route) {
router.navigateTo(currentContext, route,
transition: TransitionType.inFromRight,
transitionDuration: const Duration(milliseconds: 500));
}
This works perfectly when the notification is received while the application is open .. but when the application is running on the background or closed and the notification is received, when clicking on the notification the application will open on its state from the background or from the start if it was closed...
I want the specific route to open when clicking on the notification even if it was in the background or closed .. how to do that? is there is a way to achieve this in flutter?

Resources