Reputation: 91
I have registered my app, added google-services.json file to my app folder. I added required depndcies etc. I did everything still when I send notification from firebase console I don't receive whether my app is minimized or foreground. I don't even see something in my debug console. My AVD do have google play services too.
Upvotes: 5
Views: 12108
Reputation: 326
1.Register your app in firebase;
2.Add google-services.json it to'android -> app';
3.Add Code in Android Mainfiest.xml inside Application
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="high_importance_channel" />
<meta-data android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/ic_launcher" />
[Please Check Image before use in mainfiest open Image]
4.Add dependencies: (Use latest version)
firebase_core: ^2.7.0
firebase_messaging: ^14.2.5
flutter_local_notifications: ^13.0.0
5.Add await Firebase.initializeApp() in main method
void main() async {
await initializeFirebase();
await getFcmToken();
await NotificationService().init();
}
Future initializeFirebase() async {
await Firebase.initializeApp(
name: "Your App Name",
options: DefaultFirebaseOptions.currentPlatform,);
}
6.I Created Notification Service Class For Push Notification:
class NotificationService {
static final NotificationService _notificationService =
NotificationService._internal();
factory NotificationService() {
return _notificationService;
}
NotificationService._internal();
AndroidNotificationChannel channel = const AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
description: 'This channel is used for important notifications.',
playSound: true,
// description
importance: Importance.high,);
final BehaviorSubject<String?> selectNotificationSubject =
BehaviorSubject<String?>();
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> init() async {
_configureSelectNotificationSubject();
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/logo');
DarwinInitializationSettings initializationSettingsDarwin =
DarwinInitializationSettings(
requestSoundPermission: true,
requestBadgePermission: true,
requestAlertPermission: true,
onDidReceiveLocalNotification: onDidReceiveLocalNotification,
);
InitializationSettings initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsDarwin,
macOS: null,
);
await flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onDidReceiveNotificationResponse: (NotificationResponse notificationResponse) {
if (notificationResponse.notificationResponseType ==
NotificationResponseType.selectedNotification) {
selectNotificationSubject.add(notificationResponse.payload);
}
},
);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onDidReceiveNotificationResponse: onSelectNotification,);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(
alert: true,
badge: true,
sound: true,
);
await FirebaseMessaging.instance
.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
initFirebaseListeners();}
void _configureSelectNotificationSubject() {
selectNotificationSubject.stream.listen((String? payload) async {
if (SharedPreferenceHelper().getUserToken() == null) {
return;
}
NotificationEntity? entity =
SharedPreferenceHelper().convertStringToNotificationEntity(payload);
debugPrint(
"notification _configureSelectNotificationSubject ${entity
.toString()}");
if (entity != null) {
setRedirectionFromForeground
}
});}
Future? onDidReceiveLocalNotification(int id, String? title, String? body, String? payload) {
if (SharedPreferenceHelper().getUserToken() == null) {
return null;
}
NotificationEntity? entity =
SharedPreferenceHelper().convertStringToNotificationEntity(payload);
debugPrint(
"notification onDidReceiveLocalNotification ${entity.toString()}");
if (entity != null) {
setRedirectionFromForeground
}
return null;}
void initFirebaseListeners() {
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
if (SharedPreferenceHelper().getUserToken() == null) {
debugPrint("userToken is Null");
return;
}
debugPrint("OnMessageOpened notification opened ${message.data}");
NotificationEntity notificationEntity =
NotificationEntity.fromJson(message.data);
setRedirectionFromForeground
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
if (GetPlatform.isIOS ||
SharedPreferenceHelper().getUserToken() == null) {
return;
}
debugPrint("Foreground notification received ${message.data}");
NotificationEntity notificationEntity =
NotificationEntity.fromJson(message.data);
debugPrint(message.data.toString());
notificationEntity.title = "App Name";
notificationEntity.body = notificationEntity.body;
showNotifications(notificationEntity);
});}
Future? onSelectNotification(NotificationResponse notificationResponse) {
if (SharedPreferenceHelper().getUserToken() == null) {
return null;
}
NotificationEntity? entity =
SharedPreferenceHelper().convertStringToNotificationEntity(notificationResponse.payload);
debugPrint("notification onSelectNotification ${entity.toString()}");
if (entity != null) {
setRedirectionFromForeground
}
return null;
}
Future<void> showNotifications(NotificationEntity notificationEntity) async {
Random random = Random();
int id = random.nextInt(900) + 10;
await flutterLocalNotificationsPlugin.show(
id,
notificationEntity.title,
notificationEntity.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channelDescription: channel.description,
icon: "@mipmap/logo",
channelShowBadge: true,
playSound: true,
priority: Priority.high,
importance: Importance.high,
styleInformation: BigTextStyleInformation(notificationEntity.body!),
),
iOS: DarwinNotificationDetails(
presentBadge: true,
presentSound: true,
presentAlert: true,
badgeNumber: 1,
)
),
payload: SharedPreferenceHelper()
.convertNotificationEntityToString(notificationEntity));}
void pushNextScreenFromForeground(NotificationEntity notificationEntity) async {
SharedPreferenceHelper preferenceHelper = SharedPreferenceHelper();
Utils.showLoader();
Tuple2<String, Object>? tuple2 = await callApi(notificationEntity);
await Utils.hideLoader();
debugPrint("current active screen ${Get.currentRoute}");
if (tuple2 != null) {
//Set Redirection
}
Get.toNamed(tuple2.item1, arguments: tuple2.item2);
}}}
Upvotes: 1
Reputation: 890
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/app_icon" />
firebase_core: ^1.4.0
firebase_messaging: ^10.0.4
await Firebase.initializeApp();
await FirebaseMessaging.instance
.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
await FirebaseMessaging.instance.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
Try this one, hope it helps. Unfortunately, I don't remember all details.
Upvotes: 5