如何在Android上从Firebase数据库中提取数据时发送通知?

时间:2016-09-29 19:35:17

标签: android firebase firebase-realtime-database firebase-notifications

我知道Firebase包含一个用于发送通知,在控制台中编写消息的部分,但我希望从表中获取值以显示通知中的值。这可能吗?

1 个答案:

答案 0 :(得分:1)

这就是我的表现:

// Firebase上下文         Firebase.setAndroidContext(本);         // URL数据库firebase        Firebase ref =新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());
        }
    });