Local Notifications Plugin showing notification twice - firebase

I want to play a custom notification sound only when app is in background or terminated state and if i implement onBackgroundMessage i am getting two notifications one automatic (default sound) and another manual(with custom sound)
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
await Firebase.initializeApp();
print('Handling a background message ${message.messageId}');
if (message.notification != null) {
print(message.notification.title);
print(message.notification.body);
flutterLocalNotificationsPlugin.show(
message.notification.hashCode,
message.notification.title,
message.notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channel.description,
playSound: true,
importance: Importance.max,
priority: Priority.high,
sound: RawResourceAndroidNotificationSound('sound'),
),
iOS: IOSNotificationDetails()
));
}
// Create a notification for this
}

Related

How to stack fcm push notification and display it when internet gets on , Flutter firebase

I am using fcm for push notification , and i am trying to stack the notifications when the internet is off ,
What is happening now is
When the app doesn't have internet and the app if receives more than one notification , when the interent is switched on , only the latest notification is shown.
What I want to achieve is
When the app doesn't have internet and the app if receives more than one notification , when the interent is switched on , I want to show all the notifications not just the latest one.
I am presently using the following code snippets.
// Background and also terminated
FirebaseMessaging.instance.getInitialMessage().then((value) =>
Navigator.push(
context, MaterialPageRoute(builder: (context) => DummyScreen())));
// Background not terminated
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print(
"The message from within the onMessageOpenedApp and i am within the home_page is : " +
message.notification!.title.toString());
Navigator.push(
context, MaterialPageRoute(builder: (context) => DummyScreen()));
});
// When app is in foreground
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
if (notification != null && android != null && !kIsWeb) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
// TODO add a proper drawable resource to android, for now using
// one that already exists in example app.
icon: 'launch_background',
),
),
);
}
});

Flutter FCM data only payload notification is not received when app is terminated

I'm using firebase_messaging: ^11.2.10 and flutter_local_notifications: ^9.4.0 to show notifications. I am sending data only notifications without notification title or body. Most of the time it works, but when the app is terminated and not used for a longer period, notifications won't show until you wake or unlock the phone.
Here is the case:
I subscribe to the Firebase background messages.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(MessagingService.subscribeToFCMBackground);
await NotificationService().init();
runApp(const MyApp());
);
}
Inside my MessagingService I get the payload data and there it shows the notification based on the value under the "type" key. Looks like this:
static Future<void> subscribeToFCMBackground(RemoteMessage message) async {
await Firebase.initializeApp();
NotificationService().showNotificationByType(_getNotificationTypeFromMessage(message));
}
static NotificationType _getNotificationTypeFromMessage(final RemoteMessage message) {
final String notificationType = message.data['type'] as String;
return notificationTypeEnumMap[notificationType]!;
}
Inside the NotificationService I create notification channel, place it inside of NotificationDetails object and pass it to showNotification method. Here is an example of android channel:
final AndroidNotificationDetails _androidNotificationDetails = AndroidNotificationDetails(
'high_importance_channel',
'High Importance Channel',
channelDescription: 'Very important notifications',
playSound: true,
sound: RawResourceAndroidNotificationSound(soundFileName),
priority: Priority.max,
importance: Importance.max,
);
...
final NotificationDetails platformChannelSpecifics = NotificationDetails(
android: _androidNotificationDetails,
iOS: _iOSNotificationDetails,
);
...
await flutterLocalNotificationsPlugin.show(
0,
'Notification title',
'Notification body',
platformChannelSpecifics,
);
Also I added this to the Android Manifest file:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="high_importance_channel" />
Do you guys have any idea how to solve this problem? I would really appreciate any advice or a solution. Thank you!

Foreground Push Notification

The push-notification forces the app to another screen without even the user clicking on the notification, whenever I send a notification using cloud messaging from firebase.
I want the user to first tap on the notification, which will then push to another screen. The code below is for the app running in the foreground, where I am facing the issue.
Ty in advance. It's for ANDROID*
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
final routeFromMessage = message.data['route'];
Navigator.of(context).pushNamed(routeFromMessage);
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
if (notification != null && android != null && !kIsWeb) {
flutterLocalNotificationsPlugin!.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel!.id,
channel!.name,
channel!.description,
// one that already exists in example app.
icon: 'launch_background',
),
),
payload: message.data['route'],
);
}
});

How to configure Firebase Messaging with latest version in Flutter?

I was using the below line of code for firebase messaging configuration for flutter noticification configuration , but now after integrating to the latest version of the firebase messaging it is giving me error
CODE LINE
messaging.configure(onMessage: (Map<String, dynamic> message){}
ERROR in DART Analysis
error: The method 'configure' isn't defined for the type 'FirebaseMessaging'.
FirebaseMessaging.configure() is removed by firebase team:
Reason:
The previous implementation of configure() caused unintended side effects if called multiple times (either to register a different handler or remove handlers). This change allows developers to be more explicit about registering handlers and removing them without affecting others via Streams.
Use FirebaseMessaging.onMessage method to get messages
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
});
Use FirebaseMessaging.onMessageOpenedApp as a replacement for onLaunch and onResume handlers.
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('A new onMessageOpenedApp event was published!');
Navigator.pushNamed(context, '/message',
arguments: MessageArguments(message, true));
});
Please check following example.
https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_messaging/firebase_messaging/example/lib/main.dart
#override
void initState() {
super.initState();
FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage message) {
if (message != null) {
Navigator.pushNamed(context, '/message',
arguments: MessageArguments(message, true));
}
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channel.description,
// TODO add a proper drawable resource to android, for now using
// one that already exists in example app.
icon: 'launch_background',
),
));
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('A new onMessageOpenedApp event was published!');
Navigator.pushNamed(context, '/message',
arguments: MessageArguments(message, true));
});
}
Building on Jitesh's answer, for me implementation of getInitialMessage is needed to make the navigation works when the app is terminated (replacement for onLaunch)
// workaround for onLaunch: When the app is completely closed (not in the background) and opened directly from the push notification
FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage message) {
print('getInitialMessage data: ${message.data}');
_serialiseAndNavigate(message);
});
// onMessage: When the app is open and it receives a push notification
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print("onMessage data: ${message.data}");
});
// replacement for onResume: When the app is in the background and opened directly from the push notification.
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('onMessageOpenedApp data: ${message.data}');
_serialiseAndNavigate(message);
});
Updated One:
FirebaseMessagingService is important to get started in the beginning of app. And for that sake, you need to follow this:
Declare this function first :
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
debugPrint("Firebase Messaging firebase is initialized");
await Firebase.initializeApp();
}
And call this function in the main(){} of app:
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
And then you will be able to use these functions:
FirebaseMessaging.onMessage method to get messages
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
});
FirebaseMessaging.onMessageOpenedApp as a replacement for onLaunch and onResume handlers.
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('A new onMessageOpenedApp event was published!');
Navigator.pushNamed(context, '/message',
arguments: MessageArguments(message, true));
});
Event handling:
Event handling has been reworked to provide a more intuitive API for developers. Foreground-based events can now be accessed via Streams:
FirebaseMessaging.onMessage Returns a Stream called when an incoming FCM payload is received whilst the Flutter instance is in the foreground, containing a [RemoteMessage].
FirebaseMessaging.onMessageOpenedApp Returns a [Stream] that is called when a user presses a notification displayed via FCM. This replaces the previous onLaunch and onResume handlers.
FirebaseMessaging.onBackgroundMessage() Sets a background message handler to trigger when the app is in the background or terminated.
IosNotificationSettings:

How to show a push-notification when the application is open(foreground), when OnMessage is triggered?

Im use Flutter and Firebase Messaging.
I im configure Firebase like in example: firebaseMessaging.configure(
onMessage: ...
onLaunch: ...
onResume: ...
)
But i wanna see push-notification even when app is open.
Roughly speaking onMessage should work like onResume. How can i do this?
onMessage: (Map<String, dynamic> message) async {
showNotification(message);
print('on message $message');
}
showNotification(Map<String, dynamic> msg) async {
var android = new AndroidNotificationDetails(
'your channel id',//channel id
"your channel name",//channel name
"your channel description",//channel desc todo set all this right
icon: 'mipmap/launcher_icon'//add your icon here
);
var iOS = new IOSNotificationDetails();
var platform = new NotificationDetails(android, iOS);
await flutterLocalNotificationsPlugin
.show(0, msg['notification']['title'], msg['notification']['body'], platform);
}
I used flutter_local_notifications: ^1.2.2 to show local notification foreground.
Additionally, if you are implementing for IOS don't forget to ask for notification permission.

Resources