Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show push notification in flutter without firebase?

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?

like image 335
Umair Avatar asked Feb 07 '20 06:02

Umair


People also ask

How do I enable push notifications on 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).

Can I send push notifications without FCM?

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.

How do I show local notifications on flutter?

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.

Can I send a push notifications without an app?

Pushed allows you to send real-time notifications without developing your own app to iOs, Android and Desktop devices.


3 Answers

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);
like image 166
Navin Kumar Avatar answered Oct 05 '22 19:10

Navin Kumar


it's not compatible with latest flutter version (null-safety) yet

like image 27
SĂ©bastien Gruhier Avatar answered Oct 05 '22 19:10

SĂ©bastien Gruhier


There are two major ways to send push notifications to a flutter application without firebase.

  1. Using flutter_local_notifications package send to notification locally

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

  1. OneSignal: is a free push notification service for mobile apps. This SDK makes it easy to integrate your Flutter iOS and/or Android apps with OneSignal and it's also used in powering mobile + web push, email, SMS & in-app messages.

the package is available on pub.dev - onesignal

like image 32
techwithsam Avatar answered Oct 05 '22 20:10

techwithsam