Add button to notification in flutter_foreground_task - flutter

how add button named "Disconnect" in AndroidNotificationOptions ?
my simple code is here .
const AndroidNotificationOptions(
channelId: 'parameter_channel',
channelName: 'Parameters',
channelDescription: 'This notification appears when the reading begaz params running.',
channelImportance: NotificationChannelImportance.LOW,
priority: NotificationPriority.LOW,
visibility: NotificationVisibility.VISIBILITY_PRIVATE,
playSound: false,
enableVibration: false,
iconData: NotificationIconData(
resType: ResourceType.mipmap,
resPrefix: ResourcePrefix.ic,
name: 'launcher',
),
),
in here im using flutter_foreground_task.

If you look at the documentation you will see an example, but in your case should be like this:
androidNotificationOptions: AndroidNotificationOptions(
channelId: 'notification_channel_id',
channelName: 'Foreground Notification',
channelDescription: 'This notification appears when the foreground service is running.',
channelImportance: NotificationChannelImportance.LOW,
priority: NotificationPriority.LOW,
iconData: const NotificationIconData(
resType: ResourceType.mipmap,
resPrefix: ResourcePrefix.ic,
name: 'launcher',
),
buttons: [
const NotificationButton(id: 'disconnect', text: 'Disconnect'),
],
),
The clicks events should be handled in the TaskHandler, like this:
class MyTaskHandler extends TaskHandler{
...
#override
void onButtonPressed(String id) {
// Called when the notification button on the Android platform is pressed.
if(id == 'disconnect'){
//stop the foreground task
//do the disconnect
}
}
...
}

Related

How can I use phone notification and custom sound in my awsome notification in flutter

Hello Guys I am working on my flutter project Where I am using awesome notification. I want to do two things.
The device notification sound will be the sound source.
The custom sound provided by me will be the sound source.
I don't know How to do point 1. However for point 2 I added notification.mp3 file in my asset folder and give soundSource: 'assets/notification.mp3'
My Notification initialize Code:
AwesomeNotifications().initialize(
null,
[
NotificationChannel(
channelKey: 'key1',
channelName: 'Recipedia',
channelDescription: 'Testing Notification',
defaultColor: const Color(0XFFff735c),
ledColor: const Color(0XFFff735c),
soundSource: 'assets/notification.mp3',
enableLights: true,
enableVibration: true,
playSound: true,
)
],
);
My createNotification code:
void OTPNotification() async {
AwesomeNotifications().createNotification(
content: NotificationContent(
id: 1,
channelKey: 'key1',
title: 'Recipedia',
body: 'We have sent you a OTP code. Please check your email.',
bigPicture: 'assets/OTP1.png',
notificationLayout: NotificationLayout.BigPicture,
)
);
}

Set counter of notification using awesome_notification flutter

I am using awesome_notifications: ^0.0.6+12 as a local notification that shows notification/information on the status bar of the device. So far the function to show a notification runs very well, but I got a problem with the counter of notification.
As we know.. when one notification pops up there will be a flag counter in our icon apps 1 and when the second notification pops up.. it will turn into 2
But, the problem is... when I click the notification from the status bar and then the app is opening, the counter is still 1 while.. it should be removed after I click it... Is there a way to solve this? here is the part of the code
notifRequirement() {
AwesomeNotifications().actionStream.listen((notification) {
if (notification.channelKey == 'basic_channel' && Platform.isIOS) {
AwesomeNotifications().getGlobalBadgeCounter().then(
(value) =>
AwesomeNotifications().setGlobalBadgeCounter(value - 1),
);
}
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (_) => MainScreen(),
),
(route) => route.isFirst,
);
});
}
I call notifRequirement inside initState and in main.dart:
AwesomeNotifications().initialize(
'resource://drawable/darisdmmodified',
[
NotificationChannel(
channelKey: 'scheduled',
channelName: 'Scheduled Notifications',
defaultColor: Colors.red,
locked: false,
importance: NotificationImportance.High,
channelShowBadge: true,
channelDescription: "description 1"),
],
);

Flutter awesome notification click open specific page

I am using Flutter awesome notifications. When the notification is clicked when the application is closed, I want to direct it to a special page within the application. What is the easiest way for me to do this?
To do this, firstly you need to initialize AwesomeNotifications before runApp and then simply put a listner to listen to the notification click:
Initialize:
AwesomeNotifications().initialize(
'resource://drawable/logo_circle_notification',
[
NotificationChannel(
channelGroupKey: 'normal_channel_group',
channelKey: 'normal_channel',
channelName: 'Normal Notifications',
channelDescription: 'Notification channel for normal notifications',
defaultColor: const Color(0xFF9D50DD),
ledColor: Colors.white
),
],
channelGroups: [
NotificationChannelGroup(
channelGroupkey: 'basic_channel_group',
channelGroupName: 'Basic group'),
],
debug: true
);
Listen:
listenActionStream(){
AwesomeNotifications().actionStream.listen((receivedAction) {
var payload = receivedAction.payload;
if(receivedAction.channelKey == 'normal_channel'){
//do something here
}
});
}
you can put that lister in the initState of your splash screen or something before navigating to Home Screen of the app.

How to schedule notification at specific time using awesome package in Flutter?

I am using an awesome Package and am trying to make a notification at a specific time using this package.
Future<void> showNotificationWithIconsAndActionButtons(int id) async {
AwesomeNotifications().initialize(
'',
[
NotificationChannel(
channelKey: 'basic_channel',
channelName: 'Basic notifications',
channelDescription: 'Notification channel for basic tests',
defaultColor: Color(0xFF9D50DD),
ledColor: Colors.white,
playSound: true,
importance: NotificationImportance.Max,
defaultRingtoneType: DefaultRingtoneType.Notification,
)
]
);
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: id,
channelKey: 'basic_channel',
title: 'Anonymous says:',
body: 'Hi there!',
payload: {'uuid': 'user-profile-uuid'},
displayOnBackground: true,
displayOnForeground: true,
),
i need to make notification in particular time.
The same plugin provides a way to schedule notifications as per the need.
Checkout this link from it's description:
https://pub.dev/packages/awesome_notifications#scheduling-a-notification
Bascially showing it at a specific time will look something like this:
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: id,
channelKey: 'scheduled',
title: 'Just in time!',
body: 'This notification was schedule to shows at ' +
(Utils.DateUtils.parseDateToString(scheduleTime.toLocal()) ?? '?') +
' $timeZoneIdentifier (' +
(Utils.DateUtils.parseDateToString(scheduleTime.toUtc()) ?? '?') +
' utc)',
notificationLayout: NotificationLayout.BigPicture,
bigPicture: 'asset://assets/images/delivery.jpeg',
payload: {'uuid': 'uuid-test'},
autoCancel: false,
),
schedule: NotificationCalendar.fromDate(date: scheduleTime));
Note: The code sample is from the plugin's Readme. I have not tested this yet.
Timer.periodic(Duration(minutes: 1), (timer) {
if (DateTime.now()== DateTime.parse("2021-07-20 20:18:04Z")){// 8:18pm
return showNotificationWithIconsAndActionButtons();
}
});
This will let it check the time every minute.

No vibrate in flutter notifications

I am using firebase cloud functions to send a notification to a particular user. This is the payload that I am sending from the functions.
var payload = {
notification: {
sound: "default",
color: "#ff3296fa",
vibrate: "300",
priority: 'high',
notificationType: "52",
title: titleToBeShown,
body: message['message'],
icon: 'ic_launcher',
},
data: {
click_action: 'FLUTTER_NOTIFICATION_CLICK',
channelId: channelID,
channelName: channelName,
channelType: channelType
},
};
I am using firebase_messaging (flutter package: https://pub.dartlang.org/packages/firebase_messaging) to receive the notifications and I have written the codes for onMessage, onLaunch and onResume methods.
So when I send a message using Admin SDK admin.messaging().sendToDevice(token, payload), it sends it without vibration and sound. How can I add vibration and sound to it? Right now, it feels like a silent notification then. Which will easily be ignored by the users. Both in android and ios, its the same problem.
The sound field does not go in the notification object. It belongs in the android and apns objects. Your payload should look like this:
var payload = {
data: {
channelId: channelID,
channelName: channelName,
channelType: channelType
},
android: {
priority: 'high',
notification: {
title: titleToBeShown,
body: message['message'],
icon: 'ic_launcher',
sound: 'default',
color: '#ff3296fa',
clickAction: 'FLUTTER_NOTIFICATION_CLICK',
// Not sure what this is supposed to be, but not a valid parameter
notificationType: '52',
},
},
apns: { ... }
};
I've filled in the Android fields, but I'm not familiar with APNS payloads. Check out the FCM documentation here for more details and you can see the available payload options for APNS here.