2016-09-29 19 views
-4

Firebaseには通知を送信するセクションがあり、コンソールにメッセージを書き込んでいますが、通知の値を表示するためにテーブルから値を取得したいと思います。これは可能ですか?FirebaseデータベースからAndroid抽出データの通知を送信するにはどうすればいいですか?

+1

これは何か? https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html –

答えて

0

これは私がやった方法です:

// 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()); 
     } 
    }); 
+0

私は2つのクラスを作成しました。 1つはFirebase DatabaseのURL用、もう1つは設定用の "Name"と "Address"の値を取得します。 –

関連する問題