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?
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);
There are two major ways to send push notifications to a flutter application without firebase.
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
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
it's not compatible with latest flutter version (null-safety) yet
Related
I want to show notification based on a condition. I tried with flutter local notification package but I was only getting the foreground and background notification. if I close the app i was not having any notification from app.
example:
app is fetching the data from real-time-database firebase and data base is getting the frequency value from hardware, if frequency is greater than 50 then show notification.
if there is any another way to implement, you can also suggest me that
part of the code:
NotificationService notificationsServices = NotificationService();
void initState(){
super.initState();
notificationsServices.intializeNotification();
}
if(_displayTemp>135 || _displayVib>135)
{
notificationsServices.sendN("Alert", _displayMsg);
}
class NotificationService {
final FlutterLocalNotificationsPlugin flutterNotificationsPlugin = FlutterLocalNotificationsPlugin();
final AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('shield');
void intializeNotification() async {
InitializationSettings initializationSettings= InitializationSettings(
android: initializationSettingsAndroid
);
await flutterNotificationsPlugin.initialize(initializationSettings);
}
void sendN(String title,String body) async {
AndroidNotificationDetails androidNotificationDetails = AndroidNotificationDetails(
'channelId 2',
'channelName',
importance: Importance.max,
priority: Priority.high,
playSound: true,
//ongoing: true
);
NotificationDetails notificationDetails = NotificationDetails(
android: androidNotificationDetails,
);
await flutterNotificationsPlugin.show(
0,
title,
body,
notificationDetails
);
}
}
I am trying to send push notifications to my flutter app.
I tested android and it worked perfectly. However, ios did not work out.
Is there any progress work that I should do for the ios setting?
I did not register for the apple developer program yet.
Please help out. Somebody help me....
I will leave the code below
import 'package:get/get.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:firebase_in_app_messaging/firebase_in_app_messaging.dart';
class NotificationPresenter extends GetxController {
FirebaseMessaging messaging = FirebaseMessaging.instance;
#override
void onInit() async{
NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: true,
badge: true,
carPlay: true,
criticalAlert: true,
provisional: true,
sound: true,
);
print(settings.authorizationStatus);
_getToken();
_onMessage();
super.onInit();
}
void _getToken() async{
String? token= await messaging.getToken();
try{
print(token);
} catch(e) {}
}
final AndroidNotificationChannel channel = const AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
description: 'This channel is used for important notifications.', // description
importance: Importance.max,
);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
void _onMessage() async{
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
await flutterLocalNotificationsPlugin.initialize(
const InitializationSettings(
android: AndroidInitializationSettings('#mipmap/ic_launcher'), iOS: IOSInitializationSettings()),
onSelectNotification: (String? payload) async {});
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channelDescription: channel.description
),
),
// payload: message.data['argument']
);
}
print('foreground ėíŠėė ëŠėė§ëĨŧ ë°ėë¤.');
print('Message data: ${message.data}');
if (message.notification != null) {
print('Message also contained a notification: ${message.notification!.body}');
}
});
}
}
There are quite a few steps in case of ios
Registering a bundle id
Registering a provision profile
Enabling push notification service for that profile
Generating an apns (Apple push notification service) key
Adding this apns to firebase
Creating an ios id in firebase
Downloading the Google service info plist and adding it to the project
These steps are all required to recieve push notification in ios
Follow this step for flutter integration for setting up the APN certificate with cloud messaging
Set up for certificate and APNs
After completing certification step download the GoogleService-Info.plist and add it using Xcode.
I am working on a flutter app. I am creating a pdf file in the "Downloads" folder, after creating the file in the "Downloads" folder, showing a notification "Download Completed" using the "local_notification" plugin.
Now I want to show that pdf when the user clicks on the notification. Can anybody guide me on how I can do that?
Below is my code
final android = AndroidNotificationDetails('0', 'Adun Accounts',
channelDescription: 'channel description',
priority: Priority.high,
importance: Importance.max,
icon: '');
final iOS = IOSNotificationDetails();
final platform = NotificationDetails(android: android, iOS: iOS);
await flutterLocalNotificationsPlugin.show(
0, // notification id
fileName,
'Download complete.',
platform);
show() has a parameter called payload , you can pass the path to the pdf file here. Add the plugin open_file in your pubspec.yaml. Then inside main.dart, add the following code
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> initNotification() async {
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('notification_icon');
final IOSInitializationSettings initializationSettingsIOS =
IOSInitializationSettings();
final MacOSInitializationSettings initializationSettingsMacOS =
MacOSInitializationSettings();
final InitializationSettings initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS,
macOS: initializationSettingsMacOS);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (String? payload) {
if (payload != null) OpenFile.open(payload);
});
}
Note: This function should not be inside any class.
I've been working on an adhan app using flutter and I'm working on the scheduled notification using flutter_local_notifications package. Using the .zonedSchedule I can only display notification once.
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
AndroidInitializationSettings androidInitializationSettings = AndroidInitializationSettings("icon");
IOSInitializationSettings iosInitializationSettings = IOSInitializationSettings();
final InitializationSettings initializationSettings = InitializationSettings(
android: androidInitializationSettings,
iOS: iosInitializationSettings);
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
and for the Notification detail
var android = AndroidNotificationDetails(
"$id", channel,
importance: Importance.max,
sound: RawResourceAndroidNotificationSound('adhan'),
playSound: true,
);
var ios = IOSNotificationDetails();
var platform = NotificationDetails(android: android, iOS: ios);
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
and used it as
_flutterLocalNotificationsPlugin.zonedSchedule(
1,
"title",
"dhuhr",
tz.TZDateTime.from(prayerTimes.dhuhr!, location),
platform,
uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation
.absoluteTime,
androidAllowWhileIdle: true,
matchDateTimeComponents: DateTimeComponents.time
);
_flutterLocalNotificationsPlugin.zonedSchedule(
2,
"title",
"Asr",
tz.TZDateTime.from(prayerTimes.asr!, location),
platform,
uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation
.absoluteTime,
androidAllowWhileIdle: true,
matchDateTimeComponents: DateTimeComponents.time
);
the problem I'm facing is that if the time matches with the current prayer time it works fine. but when it's time for the next schedule it doesn't display anything.
So how can I display notification multiple times in a day?
Flutter isolates using the flutter isolate package do not show notificaitons when the app has been killed.
Tried using the isolate package with dart:isolate, that does not work.
import 'dart:async';
import 'dart:isolate';
import 'package:flutter/material.dart';
import 'package:flutter_isolate/flutter_isolate.dart';
'package:flutter_local_notifications/flutter_local_notifications.dart';
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
Future onSelectNotification(String payload) async {
if (payload != null) {
debugPrint('notification payload: ' + payload);
}
}
void isolated(String args) async {
Timer.periodic(new Duration(seconds: 10), (Timer t) => print('hi!'));
Timer.periodic(new Duration(seconds: 10), (Timer y) async
{
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your channel id', 'your channel name', 'your channel description',
importance: Importance.Max,
priority: Priority.High,
ticker: 'ticker');
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, 'plain title', 'plain body', platformChannelSpecifics,
payload: 'item x');
});
}
void main() async {
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
// If you have skipped STEP 3 then change app_icon to #mipmap/ic_launcher
var initializationSettingsAndroid =
new AndroidInitializationSettings('appicon');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);
isolated("hi");
}
The code above is what the system sees when the app has been killed as the state classes have terminated, and no longer exist. If we can get the code above running then the isolate will still work when the app has been killed by the user in Android and iOS and the state classes are no longer there.
Thanks in advance.
Rob.
Yeah it can't be done in Flutter alone as the isolates are spawned by the parent process and they get killed when the app is terminated.
for Android use services which get spawned and persist after the app dies.
For iOS you can run a background thread which gets spawned after a minimum amount of time, but there's no guarantee as to when that will run. You can, at Apple's discretion, run apps as audio or location based, which will allow the background thread to run continuously.