问题
I build an application that book lessons, in the application there are student account and teacher account. So I need when the student book the lesson then the teacher receive a notification with the details of the lesson. I use Firebase Realtime database to save the data (lesson information,...) So, the question how to send notification to the teacher after booking the lesson by the teacher. The code written with JAVA :
Here is the code the book the lesson in the database In this section after booking the lesson, the student receive notification to his phone with the details of the lesson :
private void createNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel1 = new NotificationChannel(
CHANNEL_1_ID,
"Channel 1",
NotificationManager.IMPORTANCE_HIGH
);
channel1.setDescription("This is Channel 1");
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel1);
}
}
public void sendOnChannel1(String Title,String Content) {
String title = Title;
String message = Content;
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setSmallIcon(R.drawable.buttonshape)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.build();
notificationManager.notify(1, notification);
}
ArrayList<Lesson> existLesson=new ArrayList<>();
List<String> keys = new ArrayList<>();
int counter=0;
public void getLessons(DataSnapshot dataSnapshot){
//insert the lessons to "existLesson" arrayList
for (DataSnapshot keyNode : dataSnapshot.getChildren()) {
keys.add(keyNode.getKey());
Lesson lesson = keyNode.getValue(Lesson.class);
existLesson.add(lesson);
}//for
}
int flag=1;
@Override
public void addLesson(final String subject, final String topic, final String date, final String time) {
flag=1;
mDatabase.addListenerForSingleValueEvent (new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
getLessons(dataSnapshot);
//Check if date and time is busy
for (Lesson lessonToCheck : existLesson) {
if (lessonToCheck.getDate().equals(date) && lessonToCheck.getTime().equals(time)) {
flag = 0;
}
}//for
if (flag == 0) {
Toast.makeText(LessonDetails.this, "date exist", Toast.LENGTH_SHORT).show();
// Check empty lessons
nearestLessons(existLesson, date, time);
} else {
if (flag == 1) {
new AlertDialog.Builder(lessonDetails)
.setTitle("Lesson Confirmation")
.setMessage("Lesson details:\nDate: "+date+"\nTime: "+time)
// Specifying a listener allows you to take an action before dismissing the dialog.
// The dialog is automatically dismissed when a dialog button is clicked.
.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String id = mDatabase.push().getKey();
Lesson lesson = new Lesson(subject, topic, date, time, id,user.getDisplayName()); //create lesson
Toast.makeText(LessonDetails.this,
subject + " - " + topic + " - " + date + " - " + time, Toast.LENGTH_SHORT).show();
mDatabase.child(id).setValue(lesson);
//send notification to the admin
sendOnChannel1(subject,date+" at: "+time);
}
})
// A null listener allows the button to dismiss the dialog and take no further action.
.setNegativeButton("Cancel", null)
.setIcon(android.R.drawable.ic_menu_view)
.show();
} //add lesson to DB
} //else
} //onDataChange
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
Now I need to add a code the send notification for the teacher(Admin account) with the details of the lesson .
来源:https://stackoverflow.com/questions/62902631/send-notification-from-device-to-another-with-firebase-in-android