gpt4 book ai didi

android - 添加 Firebase 子项时在 Android 中获取通知

转载 作者:行者123 更新时间:2023-11-29 19:30:26 25 4
gpt4 key购买 nike

当使用监听器将 Fire 基础数据库子项添加到数据库时,我正在尝试获取 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);
}
}

最佳答案

你应该使用 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) {

}
});

确保您可以从数据库中读取数据(检查安全规则)。

关于android - 添加 Firebase 子项时在 Android 中获取通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40063610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com