【问题标题】:How to send a notification on Android extracting data from the Firebase Database?如何在 Android 从 Firebase 数据库中提取数据时发送通知?
【发布时间】:2016-09-29 19:35:16
【问题描述】:

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

【问题讨论】:

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


【解决方案1】:

我就是这样做的:

//Firebase 上下文 Firebase.setAndroidContext(this); //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 数据库中的 URL,另一个用于设置和获取值“名称”和“地址”。
猜你喜欢
  • 2020-01-04
  • 1970-01-01
  • 2017-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-28
  • 2019-03-31
相关资源
最近更新 更多