英文:
How I add schedule notification to my flutter code using Firebase messaging?
问题
以下是代码的翻译部分:
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
? '下次疫苗接种日期'
: 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);
//调用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("按钮"),
),
),
)
],
),
),
);
}
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',
//希望在点击消息时打开该页面(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("错误的推送通知");
}
}
}
希望这些翻译有帮助!如果您有任何其他问题,请随时提问。
英文:
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 ?
答案1
得分: 1
Firebase Cloud Messaging不支持推送通知的调度。但是,你可以使用flutter_local_notifications库在本地调度通知。你可以从Firebase发送一个数据消息,然后当你的应用接收到它时,使用本地通知包根据scheduledTime
来调度通知。
英文:
Unfortunately, Firebase Cloud Messaging does not support push notifications scheduling. However, you can schedule notifications locally with flutter_local_notifications library. You can send a data message from Firebase then when your app receives it, you use the local notifications package to schedule the notification based on scheduledTime
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论