如何在 Android 从 Firebase 数据库中提取数据时发送通知?
How to send a notification on Android extracting data from the Firebase Database?
我知道 Firebase 包含一个发送通知的部分,在控制台中写入您的消息,但我想从 table 获取值以在通知中显示这些值。这可能吗?
我是这样做的:
//Firebase 上下文
Firebase.setAndroidContext(这个);
//URL 数据库 firebase
Firebase ref = new Firebase(Config.FIREBASE_URL);
ref.addValueEventListener(new ValueEventListener() {
//DataSnapshot para leer datos de un bd
@Override
public void onDataChange(DataSnapshot snapshot) {
//Get Actual Value(getchildren)
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
//Getting the data from snapshot
Person person = postSnapshot.getValue(Person.class);
//Intent(Get components GUI)
Intent intent = new Intent();
//Allow External Application(PendingIntent)
PendingIntent pInent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
//Notificacion
Notification noti = new Notification.Builder(MainActivity.this)
//Propiedades
.setContentTitle("Notificacion")
.setSound(Uri.EMPTY)
.setContentText("Nombre: "+person.getName()+"\t\tDireccion: "+person.getAddress())
.setSmallIcon(R.mipmap.bus)
.setContentIntent(pInent).getNotification();
//Cancel notification
noti.flags = Notification.FLAG_AUTO_CANCEL;
//Get Notification
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(1, noti);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
我知道 Firebase 包含一个发送通知的部分,在控制台中写入您的消息,但我想从 table 获取值以在通知中显示这些值。这可能吗?
我是这样做的:
//Firebase 上下文 Firebase.setAndroidContext(这个); //URL 数据库 firebase Firebase ref = new Firebase(Config.FIREBASE_URL);
ref.addValueEventListener(new ValueEventListener() {
//DataSnapshot para leer datos de un bd
@Override
public void onDataChange(DataSnapshot snapshot) {
//Get Actual Value(getchildren)
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
//Getting the data from snapshot
Person person = postSnapshot.getValue(Person.class);
//Intent(Get components GUI)
Intent intent = new Intent();
//Allow External Application(PendingIntent)
PendingIntent pInent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
//Notificacion
Notification noti = new Notification.Builder(MainActivity.this)
//Propiedades
.setContentTitle("Notificacion")
.setSound(Uri.EMPTY)
.setContentText("Nombre: "+person.getName()+"\t\tDireccion: "+person.getAddress())
.setSmallIcon(R.mipmap.bus)
.setContentIntent(pInent).getNotification();
//Cancel notification
noti.flags = Notification.FLAG_AUTO_CANCEL;
//Get Notification
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(1, noti);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});