create a calendar reminder in flutter - android

I am new to flutter. Recently I am doing an app whose main function is add reminders to the Calendar. I am now using the "device_calendar" package to add events into the Calendar app, the problem is that I can create an event but I just can't create a reminder to the event. For example, I created an event at 4:30 p.m., I can only see it in the Calendar App, it won't remind me when it actually comes to 4:30 p.m., which causes a lot of inconvenience.
Can someone tell me how to add reminders using "device_calendar" package, or just tell me some methods about adding events and reminders into the Calendar App with flutter.
Thanks in advance!

I think you can handle with flutter_local_notification
Schedule localnotification:
var scheduledNotificationDateTime =
new DateTime.now().add(new Duration(seconds: 5));
var androidPlatformChannelSpecifics =
new AndroidNotificationDetails('your other channel id',
'your other channel name', 'your other channel description');
var iOSPlatformChannelSpecifics =
new IOSNotificationDetails();
NotificationDetails platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.schedule(
0,
'scheduled title',
'scheduled body',
scheduledNotificationDateTime,
platformChannelSpecifics);

Related

Firebase local notification in background in Flutter

I am trying to implement local Firebase notifications in background in Android with Flutter.
Following this tutorial, I was able to get my notifications successfully set up when the app is in foreground. But while the app is in background, I do see the local notifications, but also the original notifications sent by Firebase (which I do not see while the app is in foreground).
This is a problem. Since our server sends multiple notifications, and I am implementing android_local_notifications to filter through them, and show only selected ones though local notification channel.
This is my implementation:
void main() {
// Register local notification channel
static final AndroidNotificationChannel androidChannel =
AndroidNotificationChannel(
'android_local_notifications',
'Android Local Notifications',
description: 'Used to show foreground notifications on Android.',
importance: Importance.max,
);
static final AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('mipmap/ic_launcher');
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(androidChannel);
flutterLocalNotificationsPlugin.initialize(
InitializationSettings(android: initializationSettingsAndroid, iOS: null),
);
// set up on background
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}
/// Handle background messages by registering a onBackgroundMessage handler.
/// When messages are received, an isolate is spawned (Android only, iOS/macOS does not require a separate isolate) allowing you to handle messages even when your application is not running.
/// https://firebase.google.com/docs/cloud-messaging/flutter/receive
#pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// Initialize firebase
await Firebase.initializeApp();
// Creates a local notification
flutterLocalNotificationsPlugin.show(
notificationHashCode,
translatedTitleString,
translatedBodyString,
NotificationDetails(
android: AndroidNotificationDetails(
androidChannel.id,
androidChannel.name,
channelDescription: androidChannel.description,
),
),
);
}
Manifest:
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver"
android:exported="true" tools:replace="android:exported"/>
How do I get to hide the original Firebase pushes while the app is in background?

Flutter when action is set on notification, the app calls BackgroundMethod even if the app is in foreground

I have a flutter app that uses local notifications https://pub.dev/packages/flutter_local_notifications.
I have a notification that is set to a certain date :
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onDidReceiveNotificationResponse: (NotificationResponse response) async {
print("FOREGORUND");
},
onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
);
await flutterLocalNotificationsPlugin.zonedSchedule(
uid.hashCode,
title,
desc,
timez.TZDateTime.from(datex, timez.local),
NotificationDetails(
android: AndroidNotificationDetails(
'your channel id', 'your channel name',
channelDescription: 'your channel description',
actions: <AndroidNotificationAction>[
AndroidNotificationAction(
"CompleteID",
"Complete",
icon: DrawableResourceAndroidBitmap('complete_img'),
showsUserInterface: false,
cancelNotification: true,
),
],)
),
payload: uid,
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime);
When I click on the complete action from the notification, the app calls "notificationTapBackground" even if the app is in foreground, so from notificationTapBackground isolate thread i can' t call setstate inside my app.
I would like to be able to click on the complete action from the notification and have :
the app calls notificationTapBackground, if the app is in the background
the app calls onDidReceiveNotificationRespons, if the app is in the foreground
all with the parameter showsUserInterface: false, inside notification actions.

Group Notifications using flutter_local_notifications package (android)

I am building a mobile app using flutter and I a struggling with grouping the notifications on android. I have it working fine on ios but not on android. Here is the code I am working with below for groupchats:
flutterLocalNotificationsPlugin.show(
notificationId++,
response.message.text == '' ? 'Message from ${response.message.user?.name} in liveroom'
: '${response.message.user?.name} in liveroom',
response.message.text,
NotificationDetails(
android: AndroidNotificationDetails(
cid,
cid,
groupKey: cid,
channelDescription: cid,
),
iOS: IOSNotificationDetails(
threadIdentifier: cid
)
),
);
And here is the code i am working with for one on one messaging:
flutterLocalNotificationsPlugin.show(
notificationId++,
response.message.text == '' ? 'Message from ${response.message.user?.name}'
: '${response.message.user?.name}',
response.message.text,
NotificationDetails(
android: AndroidNotificationDetails(
cid,
cid,
groupKey: cid,
channelDescription: cid,
setAsGroupSummary: true
),
iOS: IOSNotificationDetails(
threadIdentifier: response.message.user?.name
)
),
);
The cid is the channel id and it unique for each group chat. It is also unique for each message thread between two people. I am trying to collapse the notifications from one person into one group. And also collapse the notifications from one group chat into one group (this is the behvaiour on ios based on the code above and i would like to replicate for android).

Flutter Local Notification only showing dot on Android

I am using Local Notifications for my app and it is working fine on my iPhone but when firing a Notification on my Android Simulator it is not showing the Notification on top of the screen but only the dot:
The Notification actually appears fine in the Notification Center:
I am making sure to init and I a calling instantNotification which looks like this:
Future initialize() async {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
AndroidInitializationSettings androidInitializationSettings =
AndroidInitializationSettings('app_icon');
IOSInitializationSettings iosInitializationSettings =
IOSInitializationSettings();
final InitializationSettings initializationSettings =
InitializationSettings(
android: androidInitializationSettings,
iOS: iosInitializationSettings);
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
}
//Instant Notifications
Future instantNofitication() async {
var android = AndroidNotificationDetails('id', 'channel', 'description');
var ios = IOSNotificationDetails();
var platform = new NotificationDetails(android: android, iOS: ios);
await _flutterLocalNotificationsPlugin.show(
0,
'Demo instant notification',
'Tap to do something',
platform,
payload: 'Welcome to demo app',
);
}
What am I missing here?
You can try setting the importance level of the notification to a maximum with this line:
importance: Importance.max
which you add to the AndroidNotificationDetails class instance. This will tell to an Android OS that notification is important for the user and a heads-up display (a little popup) on top of the screen should be displayed for a few seconds.
I think that will solve your problem after reading an Android notification documentation
Adding on to #Antonio Valentic's answer, add the following properties to the AndroidNotificationDetails
importance: Importance.max,
priority: Priority.max,
fullScreenIntent: true,
enableVibration: true,
playSound: true
As quoted from the link:
The user's activity is in fullscreen mode (the app uses fullScreenIntent).
The notification has high priority and uses ringtones or vibrations
on devices running Android 7.1 (API level 25) and lower.
The notification channel has high importance on devices running Android 8.0 (API level 26) and higher.

flutter local notification: what's the channel id?

I'm using flutter_local_notifications, and to create a notfication (let's focus on android ) you do the following:
var androidPlatformChannelSpecifics =
new AndroidNotificationDetails(
'your other channel id',
'your other channel name',
'your other channel description');
var iOSPlatformChannelSpecifics =
new IOSNotificationDetails();
NotificationDetails platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
As you can see in the android case you provide 3 parameters related to a channel
So my quesiton is what this channel is used for and why in android we need to provide an id, a name and a description to it ?
notification channels give us the ability to group notifications and let user interact with those channels.
Let's assume you are building a chat application, you can group messages coming from Alice under channel channel-alice, and you can only mute channel-alice or do different actions to it.
Channels are required after API level 26.

Categories

Resources