In my flutter application, i have to show push notification without firebase. My server will send me a message after hitting a particular API, and that message i want to show as a push notification.
Can you show me a way how can i do it in flutter?
To send a notification, go to Firebase Console → Cloud Messaging and click on Send your first message. Then enter the Title and body field. If you wish to send it to a particular device then click on Send test message and enter the FCM registration token. (Enter the current FCM registration token).
It's definitely possible -- you don't have to use Firebase to deliver push notifications. You can roll out your own notification solution or consider a paid product such as Pushy (pushy.me) which does not rely on Firebase Cloud Messaging.
After your creating your flutter project it is necessary to add the “flutter_local_notifications” package into your pubspec. yaml file under dependencies. It will help you to effectively deal with the Push Notification tasks. Then import the package into the necessary place of coding.
Pushed allows you to send real-time notifications without developing your own app to iOs, Android and Desktop devices.
You can use Local notification plugin
https://pub.dev/packages/flutter_local_notifications
After your API response, just show that data in your local notification
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin(); // initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
var initializationSettingsAndroid =
new AndroidInitializationSettings('app_icon');
var initializationSettingsIOS = IOSInitializationSettings(
onDidReceiveLocalNotification: onDidReceiveLocalNotification);
var initializationSettings = InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);
it's not compatible with latest flutter version (null-safety) yet
There are two major ways to send push notifications to a flutter application without firebase.
example code:
@override
void initState() {
super.initState();
var initializationSettingsAndroid = AndroidInitializationSettings('ypur-icon-name(icon)');
var initializationSettingsIOS = IOSInitializationSettings();
var initializationSettings = InitializationSettings(
android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: _onNotificationClicked);
}
Future _showNotification() async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'Channel id', 'Your notification ID', 'Notification name',
importance: Importance.defaultImportance,
priority: Priority.defaultPriority);
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'Notification Alert đź””',
'Message - There is a new notification on your account, kindly check it out',
platformChannelSpecifics,
payload:
'Message - There is a new notification on your account, kindly check it out',
);
}
}
Then you create a function or just show a dialog when the notification is clicked e.g
Future _onNotificationClicked() async {
return showDialog();
);
And that for flutter local notification, you can check the package documentation for more info, another approach you can make use of is to connect this local notification to your database/server or API so it can be on call of your API that the user would receive the notification. etc
the package is available on pub.dev - onesignal
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With