Here it is my UI
I successfully implemented Firebase notifications. When I add a title, description, and date, and click the button, a notification is sent from Firebase to my device. However, I want to receive a notification based on the date and time I set within the application.
notification screenshot
my codeUI code
DateTime now = new DateTime.now();
void showDatePickerNextVaccinationDate() {
DateTime mindate = DateTime(now.year , now.month, now.day);
DateTime maxdate = DateTime(now.year + 12, now.month, now.day);
showCupertinoModalPopup(
context: context,
builder: (BuildContext builder) {
return Container(
height: MediaQuery.of(context).copyWith().size.height * 0.25,
color: Colors.white,
child: CupertinoDatePicker(
maximumYear: now.year + 12,
minimumYear: now.year ,
mode: CupertinoDatePickerMode.dateAndTime,
initialDateTime: mindate,
onDateTimeChanged: (value) {
if (value != nextDate) {
setState(() {
nextDate = value;
//calAge();
});
}
},
maximumDate: maxdate,
minimumDate: mindate,
),
);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
controller: title,
),
TextFormField(
controller: body,
),
GestureDetector(
onTap: showDatePickerNextVaccinationDate,
child: SizedBox(
width: 500,
height: 70,
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
borderRadius: BorderRadius.circular(15),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: EdgeInsets.only(
left: 3,
top:20,
bottom: 10,
),
child: Text(
nextDate == null
? 'Next Vaccinated Date'
: DateFormat('HH MMMM dd yyyy ')
.format(nextDate!),
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w300,
),
),
),
Icon(
Icons.arrow_drop_down,
color: Colors.grey[700],
size: 30.0,
),
],
),
),
),
),
GestureDetector(
onTap: () async {
String name = "User1";
String titleText = title.text;
String bodyText = body.text;
String selectedNextDate = nextDate.toString();
if (name != "") {
DocumentSnapshot snap = await FirebaseFirestore.instance
.collection("UserTokens")
.doc(name)
.get();
String token = snap['token'];
print(token);
//call sendPushMessage
sendPushMessage(token, titleText, bodyText,selectedNextDate);
}
},
child: Container(
margin: EdgeInsets.all(20),
height: 40,
width: 200,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Colors.redAccent.withOpacity(0.5),
)
]),
child: Center(
child: Text("button"),
),
),
)
],
),
),
);
}
}
function
void sendPushMessage(String token, String body, String title,String selectedNextDate) async {
try {
await http.post(Uri.parse('https://fcm.googleapis.com/fcm/send'),
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'key=....',
},
body: jsonEncode(<String, dynamic>{
'priority': 'high',
//want this to when click message open that page (payload function)
'data': <String, dynamic>{
'click_action': 'Test_Notification',
'status': 'done',
'body': '$body $selectedNextDate',
'title': title,
"isScheduled" : "true",
"scheduledTime" : "2023-05-17 18:44:00"
},
"notification": <String, dynamic>{
"title": title,
"body":'$body $selectedNextDate',
"android_channel_id": "dbfood",
"isScheduled" : "true",
"scheduledTime" : "2023-05-17 18:44:00"
},
"to": token,
}));
} catch (e) {
if (kDebugMode) {
print("Error push notification");
}
}
}
How I solve this ?