在添加Firebase子项后,在Android中获取通知 [英] Get a Notification in Android when a Firebase child has been added
本文介绍了在添加Firebase子项后,在Android中获取通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图通过使用侦听器将Fire base Database子数据添加到数据库时得到一个android通知,但无法获取通知。我已经编写了这个小测试应用程序,它不显示通知,当应用程序运行,甚至在后台。有人可以看看这个,帮助我,我仍然是一个初学者,一点点的帮助将是美好的!
包com.fayaz.firebasenotify;
导入android.content.Context;
导入android.support.v7.app.AppCompatActivity;
导入android.os.Bundle;
导入android.util.Log;
导入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;
导入android.app.NotificationManager;
导入android.support.v4.app.NotificationCompat;
导入android.view.View;
public class MainActivity extends AppCompatActivity {
$ b $ 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推送通知);
builder.setContentText(您好,这是一个测试Firebase通知,新增了一个数据库子项);
NotificationManager notificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1,builder.build());
$ b @Override
public void onCancelled(DatabaseError databaseError){
Log.i(FirebaseError,databaseError.getMessage());
}
};
myRef.addValueEventListener(valueEventListener);
$ div $解析方案
应该使用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推送通知);
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());
}
$ b @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){
$ b}
});
确保您可以从数据库读取数据(检查安全规则)。 b $ b
I am trying to get an android notification when a Fire base Database child has been added to the database using listeners, but unable to get the notification. I have coded this little test app which doesn't show an notification when the app is run, or even on background. Can someone please look into this, and help me out, I am still a beginner, a little help would be wonderful!
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);
}
}
解决方案
You should use 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) {
}
});
Make sure you can read the data from the database (check security rules).
这篇关于在添加Firebase子项后,在Android中获取通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文