You are on page 1of 4

class LocalNotificationService{

static final FlutterLocalNotificationsPlugin _notificationsPlugin =


FlutterLocalNotificationsPlugin();
static void createanddisplaynotification(RemoteMessage message) async {
try {
final id = DateTime.now().millisecondsSinceEpoch ~/ 1000;
NotificationDetails notificationDetails = NotificationDetails(
android: AndroidNotificationDetails(
"High_importance_channel",
"High_importance_channel",
importance: Importance.max,
channelShowBadge: true,
priority: Priority.high,
),
);

await _notificationsPlugin.show(
id,
message.notification!.title,
message.notification!.body,
notificationDetails,
payload: message.data['_id'],
);
} on Exception catch (e) {
print(e);
}
}

---------------------------------------------------
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {

await Firebase.initializeApp();

const AndroidNotificationChannel channel = AndroidNotificationChannel(


"High_importance_channel",
"High_importance_channel",
importance: Importance.high,
);

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =


FlutterLocalNotificationsPlugin();
const InitializationSettings initializationSettings =
InitializationSettings(
android: AndroidInitializationSettings("@mipmap/ic_launcher"),
);

void main () async {


WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(ProviderScope(child: Home()));
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>(
)
?.createNotificationChannel(channel);
flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onSelectNotification: (String? id) async {
if (id!.isNotEmpty) {
print("Router Value1234 $id");

// Navigator.of(context).push(
// MaterialPageRoute(
// builder: (context) => DemoScreen(
// id: id,
// ),
// ),
// );

}
},
);

--------------------------------------------------

@override
void initState() {
super.initState();

// 1. This method call when app in terminated state and you get a notification
// when you click on notification app open from terminated state and you can
get notification data in this method

FirebaseMessaging.instance.getInitialMessage().then(
(message) {
print("FirebaseMessaging.instance.getInitialMessage");
if (message != null) {
print("New Notification");
// if (message.data['_id'] != null) {
// Navigator.of(context).push(
// MaterialPageRoute(
// builder: (context) => DemoScreen(
// id: message.data['_id'],
// ),
// ),
// );
// }
LocalNotificationService.createanddisplaynotification(message);
}
},
);

// 2. This method only call when App in forground it mean app must be opened
FirebaseMessaging.onMessage.listen(
(message) {
print("FirebaseMessaging.onMessage.listen");
if (message.notification != null) {
print(message.notification!.title);
print(message.notification!.body);
print("message.data11 ${message.data}");
LocalNotificationService.createanddisplaynotification(message);

}
},
);

// 3. This method only call when App in background and not terminated(not
closed)
FirebaseMessaging.onMessageOpenedApp.listen(
(message) {
print("FirebaseMessaging.onMessageOpenedApp.listen");
if (message.notification != null) {
print(message.notification!.title);
print(message.notification!.body);
print("message.data22 ${message.data['_id']}");
LocalNotificationService.createanddisplaynotification(message);
}
},
);

// getToken();
}

// Future<void> getToken()async{
// final response = await FirebaseMessaging.instance.getToken();
// print(response);
// }

------------------------------------------------------------------------
<meta-data

android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="High_importance_channel" />

try{
final response = await dio.post('https://fcm.googleapis.com/fcm/send',
data: {
"notification": {
"title": "hello firbase project",
"body": message.text,
"android_channel_id": "High_importance_channel"
},
"to":
"fzL5E8xZQ9654MiZ2Sdua3:APA91bGfUI89fdMzwVwdjJNBGMSDQTZBlaPkC9k67nAF06F4nCq1qtbVmlL
k8FwM1sUih0_kKEj-fv6Y8RQMgJHG5zrfKc2Q_I1cQ8d9g0zG4nadvBNnSQDiPVSgjCGNf9lpmpCH5o01"

}, options: Options(
headers: {
HttpHeaders.authorizationHeader :
'key=AAAAWyskt5c:APA91bFpl2KNnos1ikmF80Z8sTq3uB7TfldnEzmcIr0YYdX3xJz21AXLK1YWNbY31q
NdL7wiEZCFzfrklTqNe8NY14A51I-zWCqXHuanugW5ukf_di55VcSGk1bynfk7BVJ17BZ9HGtC'
}
)
);
print(response.data);

}on FirebaseException catch (err){


print(err);
}

You might also like