Local Notifications
Local notifications are scheduled and displayed by the app itself. They do not require an internet connection or external server. Common use cases include reminders, alarms, and notifications triggered by in-app events.
Popular Local Notification Packages for Flutter:
-
- Description: A highly customizable package for displaying notifications locally on both Android and iOS.
- Features: Schedule notifications, display notifications immediately, customize notification appearance, manage notification channels (Android), and more.
- Example Usage:
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); void main() { runApp(MyApp()); const AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher'); final InitializationSettings initializationSettings = InitializationSettings(android: initializationSettingsAndroid); flutterLocalNotificationsPlugin.initialize(initializationSettings); } Future _showNotification() async { const AndroidNotificationDetails androidPlatformChannelSpecifics = AndroidNotificationDetails( 'your channel id', 'your channel name', 'your channel description', importance: Importance.max, priority: Priority.high, showWhen: false); const NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics); await flutterLocalNotificationsPlugin.show( 0, 'Hello', 'This is a local notification', platformChannelSpecifics, payload: 'item id 2'); }
- awesome_notifications https://pub.dev/packages/awesome_notifications
- Description: A complete solution for local and push notifications with rich features and cross-platform support.
- Features: High customizability, rich media notifications, scheduled notifications, background notifications, and more.
- Example Usage
import 'package:awesome_notifications/awesome_notifications.dart'; void main() { AwesomeNotifications().initialize( 'resource://drawable/res_app_icon', [ NotificationChannel( channelKey: 'basic_channel', channelName: 'Basic notifications', channelDescription: 'Notification channel for basic tests', defaultColor: Color(0xFF9D50DD), ledColor: Colors.white) ] ); runApp(MyApp()); } Future _showNotification() async { await AwesomeNotifications().createNotification( content: NotificationContent( id: 10, channelKey: 'basic_channel', title: 'Hello', body: 'This is a local notification' ) ); }
Remote Notifications
Remote notifications, also known as push notifications, are sent from a server to a device via a push notification service. These require an internet connection and are typically used for alerts, updates, messages, and more.
Popular Remote Notification Providers for Flutter:
-
Firebase Cloud Messaging (FCM):
- Description: A robust and scalable solution from Google for sending notifications and messages to users.
- Features: Supports both direct messages and topic-based messaging, advanced analytics, targeting, and more.
- Example Usage:
import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); FirebaseMessaging messaging = FirebaseMessaging.instance; messaging.getToken().then((String? token) { assert(token != null); print("FirebaseMessaging token: $token"); }); FirebaseMessaging.onMessage.listen((RemoteMessage message) { print('Got a message whilst in the foreground!'); print('Message data: ${message.data}'); if (message.notification != null) { print('Message also contained a notification: ${message.notification}'); } }); runApp(MyApp()); }
Flutter Fire
Flutter Fire: https://firebase.google.com/docs/flutter/setup?platform=ios is an ecosystem of tools that allows you to setup your project with firebase – not only about notifications and firebase_messaging, but to all other services they offer.
- OneSignal:
- Description: A popular cross-platform service for push notifications and in-app messaging.
- Features: Rich media notifications, segmentation, analytics, and more.
- Example Usage
import 'package:onesignal_flutter/onesignal_flutter.dart'; void main() { WidgetsFlutterBinding.ensureInitialized(); OneSignal.shared.setAppId("your-onesignal-app-id"); OneSignal.shared.setNotificationReceivedHandler((OSNotification notification) { print('Received notification: ${notification.jsonRepresentation()}'); }); OneSignal.shared.setNotificationOpenedHandler((OSNotificationOpenedResult result) { print('Opened notification: ${result.notification.jsonRepresentation()}'); }); runApp(MyApp()); }
- Pusher Beams:
- Description: A scalable and easy-to-integrate service for real-time push notifications.
- Features: Supports targeted notifications, user and device management, and more.
- Example Usage:
import 'package:pusher_beams/pusher_beams.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); PusherBeams.instance.start('your-instance-id'); PusherBeams.instance.addDeviceInterest('hello'); runApp(MyApp()); } void sendNotification() { PusherBeams.instance .publishToInterests( interests: ['hello'], publishBody: { 'apns': {'aps': {'alert': 'Hello'}}, 'fcm': {'notification': {'title': 'Hello', 'body': 'This is a push notification'}} }); }
- Amazon Amplify Push Notifications
Amazon offer their own alternative for push notifications, authentication, storage and more. For Messages, check out this tutorial: https://docs.amplify.aws/gen1/flutter/build-a-backend/push-notifications/set-up-push-notifications/
- Huawei Push Kit
Huawei have been blocked by Americans, because it is chinease. But, there are 4 times more people there than in USA.
Huawei Push Kit provides a cloud-based service to send messages to devices running on Huawei Mobile Services (HMS). It is a good alternative for apps targeting Huawei devices.
Setting up Huawei Push Kit:
-
Register on Huawei Developer Console:
- Create a project and enable the Push Kit service.
-
Obtain Credentials:
- Get your App ID, App Secret, and Push Kit credentials.
-
Integrate with Flutter:
- Use the
huawei_push
package to integrate Huawei Push Kit into your Flutter app.
- Use the
Example Usage:
import 'package:flutter/material.dart'; import 'package:huawei_push/huawei_push.dart'; void main() { runApp(MyApp()); setupHuaweiPush(); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Huawei Push Example')), body: Center(child: Text('Huawei Push Example')), ), ); } } void setupHuaweiPush() async { await Push.init(); await Push.turnOnPush(); Push.getTokenStream.listen((token) { print('Push Token: $token'); }); Push.onMessageReceivedStream.listen((RemoteMessage remoteMessage) { print('Received remote message: ${remoteMessage.data}'); }); } Future sendPushNotification(String title, String body) async { // Use Huawei Cloud functions or your backend to send push notifications using the Push Kit APIs // You would typically make an HTTP POST request to the Huawei Push Kit API }
Choosing the proper provider needs to be according to:
- target app store
- target market
Read more Flutter articles in my blog https://programtom.com/dev/?s=flutter