1

Fire베이스 데이터베이스 하위가 리스너를 사용하여 데이터베이스에 추가되었지만 알림을 얻을 수 없을 때 Android 알림을 받으려고합니다. 앱을 실행할 때 또는 백그라운드에서 알림을 표시하지 않는 작은 테스트 앱을 코딩했습니다. 누군가가 이것을 들여다보고 나를 도울 수 있습니까, 나는 초보자입니다. 약간의 도움이 멋질 것입니다!Firebase 하위가 추가되었을 때 Android에서 알림 받기

package com.fayaz.firebasenotify; 

import android.content.Context; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import com.google.firebase.auth.FirebaseAuth; 
import com.google.firebase.database.DataSnapshot; 
import com.google.firebase.database.DatabaseError; 
import com.google.firebase.database.DatabaseReference; 
import com.google.firebase.database.FirebaseDatabase; 
import com.google.firebase.database.ValueEventListener; 
import android.app.NotificationManager; 
import android.support.v4.app.NotificationCompat; 
import android.view.View; 


public class MainActivity extends AppCompatActivity { 

private FirebaseDatabase myFirebaseRef = FirebaseDatabase.getInstance(); 
private DatabaseReference myRef = myFirebaseRef.getReference(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

} 

NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 

public void sendNotification(View view) { 
    ValueEventListener valueEventListener = new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      builder.setSmallIcon(R.mipmap.ic_launcher); 
      builder.setContentTitle("Firebase Push Notification"); 
      builder.setContentText("Hello this is a test Firebase notification, a new database child has been added"); 
      NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
      notificationManager.notify(1, builder.build()); 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 
      Log.i("FirebaseError", databaseError.getMessage()); 
     } 

    }; 
    myRef.addValueEventListener(valueEventListener); 
} 
} 

답변

2

당신은 ChildEventListener를 사용해야합니다 ,

super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mRootRef = FirebaseDatabase.getInstance().getReference(); 
    builder = new NotificationCompat.Builder(this); 
    mRootRef.addChildEventListener(new ChildEventListener() { 
     @Override 
     public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
      builder.setSmallIcon(R.mipmap.ic_launcher); 
      builder.setContentTitle("Firebase Push Notification"); 
      builder.setContentText("Hello this is a test Firebase notification, a new database child has been added"); 
      NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
      notificationManager.notify(1, builder.build()); 
     } 

     @Override 
     public void onChildChanged(DataSnapshot dataSnapshot, String s) { 

     } 

     @Override 
     public void onChildRemoved(DataSnapshot dataSnapshot) { 

     } 

     @Override 
     public void onChildMoved(DataSnapshot dataSnapshot, String s) { 

     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 

(보안 규칙을 확인) 데이터베이스에서 데이터를 읽을 수 있는지 확인합니다.

+0

안녕하세요. T.SoChildAdded()를 추가하려고했으나 작동하지 않는 것 같습니다! 나에게 PM을 [email protected]으로 보내 주시겠습니까? 앱이 실행되고 있지 않을 때 알림을 받으려면 Listener가 필요하지 않습니다. –

+0

안녕하세요. T.S, 작동하게했습니다. 도와 주셔서 정말로 고맙습니다! –

+1

내가 새 메시지를 받으면 앱이 백그라운드에있을 때 알림을 생성 할 수 있습니까? – user512