Here is the Class of Local Notification Service which will Call while the Notification Came
class LocalNotificationService {
static final FlutterLocalNotificationsPlugin _notificationsPlugin =
FlutterLocalNotificationsPlugin();
static void initialize() {
// initializationSettings for Android
const InitializationSettings initializationSettings =
InitializationSettings(
android: AndroidInitializationSettings("@mipmap/ic_launcher"),
);
_notificationsPlugin.initialize(
initializationSettings,
onDidReceiveBackgroundNotificationResponse: (details) {
print("details Data : ${details.payload}");
if (details.payload == 'Offer Section' && details.payload != null) {
if (tokenValue != null) {
Navigator.push(
navigatorkey.currentContext!,
MaterialPageRoute(
builder: (context) => OfferMeScreen(),
));
}
} else if (details.payload == 'Surprise Me Section' &&
details.payload != null) {
Navigator.push(
navigatorkey.currentContext!,
MaterialPageRoute(
builder: (context) => HomeScreen(index: 2),
));
} else if (Provider.of<GeneralController>(navigatorkey.currentContext!,
listen: false)
.notification_name ==
'Restaurant' &&
Provider.of<GeneralController>(navigatorkey.currentContext!,
listen: false)
.notification_name !=
null) {
Provider.of<FoodController>(navigatorkey.currentContext!,
listen: false)
.restaurantDetailApi(navigatorkey.currentContext!,
id: details.payload);
}
},
onDidReceiveNotificationResponse: (details) {
print("details Data : ${details.payload}");
if (details.payload == 'Offer Section' && details.payload != null) {
if (tokenValue != null) {
Navigator.push(
navigatorkey.currentContext!,
MaterialPageRoute(
builder: (context) => OfferMeScreen(),
));
}
} else if (details.payload == 'Surprise Me Section' &&
details.payload != null) {
Navigator.push(
navigatorkey.currentContext!,
MaterialPageRoute(
builder: (context) => HomeScreen(index: 2),
));
} else if (Provider.of<GeneralController>(navigatorkey.currentContext!,
listen: false)
.notification_name ==
'Restaurant' &&
Provider.of<GeneralController>(navigatorkey.currentContext!,
listen: false)
.notification_name !=
null) {
Provider.of<FoodController>(navigatorkey.currentContext!,
listen: false)
.restaurantDetailApi(navigatorkey.currentContext!,
id: details.payload);
}
},
);
}
static void createanddisplaynotification(RemoteMessage message) async {
try {
final id = DateTime.now().millisecondsSinceEpoch ~/ 1000;
const NotificationDetails notificationDetails = NotificationDetails(
android: AndroidNotificationDetails(
"ikwteat_push_notification",
"ikwteatpushnotificationappchannel",
importance: Importance.max,
priority: Priority.high,
),
);
Map<String, dynamic> data = message.data;
String linkTo = data['notification_link_to'];
if (linkTo == 'Restaurant') {
print('object');
Provider.of<GeneralController>(navigatorkey.currentContext!,
listen: false)
.updateNotification(value: linkTo);
if (data['restaurant_id'] != null) {
linkTo = data['restaurant_id'];
print("Restaurant Id : ${linkTo}");
}
}
print('Notfication id ${id.runtimeType}');
await _notificationsPlugin.show(
id,
message.notification!.title,
message.notification!.body,
notificationDetails,
payload: linkTo,
);
} on Exception catch (e) {
print(e);
}
}
}
Here are the Firebase Function which Are handling different states of Notifications like Background , foreground and Terminated
void FirebaseFunctions() {
// 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");
}
},
);
FirebaseMessaging.onMessage.listen(
(message) {
print("FirebaseMessaging.oneMssage.listen");
if (message.notification != null) {
print(message.notification!.title);
print(message.notification!.body);
Map<String, dynamic> data = message.data;
String linkTo = data['notification_link_to'];
print("Link to ${linkTo}");
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);
}
},
);
}
when the Notification come it keeps giving me the Error like
D/CompatibilityChangeReporter( 2009): Compat change id reported: 160794467; UID 11103; state: ENABLED
E/MethodChannel#dexterous.com/flutter/local_notifications( 2009): Failed to handle method call
E/MethodChannel#dexterous.com/flutter/local_notifications( 2009): java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
I couldn't understand where i am reading integer value if any body knows where i am going wrong please help me out