I am doing a flutter project and in this project is needed a notification, however when sending the notification it shows it normally if the app is background or closed but it does not show when the app is open.
here is the AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.eduflexdchedule"
android:name="android.permission.INTERNET">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.USE_BIOMETRIC"/>
<uses-permission android:name="android.permission.USE_FINGERPRINT"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:label="Label"
android:icon="@mipmap/ic_launcher">
<service android:name="com.google.firebase.messaging.FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"
android:usesCleartextTraffic="true">
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>
And here is the code on the main.dart
import 'package:project_app/API/Notificator.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import
'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:native_notify/native_notify.dart';
import 'App.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// NativeNotify.initialize(96, 'cVRYxxIMoJ4g4UXxgCbr2g');
Firebase.initializeApp();
await SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp],
);
late final FirebaseMessaging _messaging;
_messaging = FirebaseMessaging.instance;
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
NotificationSettings settings = await _messaging.requestPermission(
alert: true,
badge: true,
provisional: false,
sound: true,
);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
// print foreground message here.
print("OVER HERE");
print('Handling a foreground message ${message.messageId}');
print('Notification Message: ${message.data}');
if (message.notification != null) {
print('Message also contained a notification:
${message.notification}');
}
//.. rest of your code
NotificationService.showNotification(message);
});
runApp(
MyApp(),
);
}
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message)
async {
print('Handling a background message ${message.messageId}');
print('Notification Message: ${message.data}');
NotificationService.showNotification(message);
}
and finally here is the notificator service:
import 'dart:io';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import
'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:stacked_services/stacked_services.dart';
class NotificationService {
static final FlutterLocalNotificationsPlugin
_flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
static Future initialize() async {
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin
= FlutterLocalNotificationsPlugin();
AndroidInitializationSettings androidInitializationSettings =
AndroidInitializationSettings('@mipmap/ic_launcher');
InitializationSettings initializationSettings =
InitializationSettings(android: androidInitializationSettings);
await flutterLocalNotificationsPlugin.initialize(
initializationSettings,
// onSelectNotification: doSomething(),
);
}
static Future showNotification(RemoteMessage message) async {
AndroidNotificationDetails androidDetails =AndroidNotificationDetails(
"111",
"channel",
enableLights: true,
enableVibration: true,
priority: Priority.high,
importance: Importance.max,
//largeIcon: DrawableResourceAndroidBitmap("ic_launcher"),
styleInformation: MediaStyleInformation(
htmlFormatContent: true,
htmlFormatTitle: true,
),
playSound: true,
);
await _flutterLocalNotificationsPlugin.show(
message.data.hashCode,
message.data['title'],
message.data['body'],
NotificationDetails(
android: androidDetails,
));
}
}
some solutions on the site were already attempted but they dont seem to show the notification on foreground
CodePudding user response:
use onMessageOpenedApp add setUpFirebase:
FirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);
_handleMessage:
void _handleMessage(RemoteMessage message) {
print(message);
}
CodePudding user response:
Android handles incoming notifications differently based on a few different factors. If the application is currently in the foreground, a visible notification is not presented.
As per FlutterFire, you can set foreground notifications for Android using flutter_local_notifications