我在childEventListener中实现了一个通知构建器,以便我的用户在新帖子可用时收到通知......通知构建器方法是:
public void showNotification() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.app_logo)
.setContentTitle("new post")
.setContentText("Check it out guys");
Intent resultIntent = new Intent(this, MainActivity.class);
android.support.v4.app.TaskStackBuilder stackBuilder = android.support.v4.app.TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int mId;
mId = 1;
mNotificationManager.notify(mId, mBuilder.build());
}
并且在onCreate()方法中我有
mDatabasePosts = FirebaseDatabase.getInstance().getReference().child("posts");
mDatabasePosts.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
showNotification();
}
@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) {
}
});
当孩子被添加到数据库时,我的用户会收到通知但他们在加载应用程序时也会收到通知...我尝试在onPause()上实现此功能,但每次按下主页按钮时都会显示通知。请告知我哪里出错。
谢谢!
答案 0 :(得分:0)
默认情况下,尽管该方法的名称,当您获得DatabaseReference时,firebase将获取所需子项的所有数据并返回。你需要做的是:
将onChildAdded方法更改为:
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if(!haveNewItems)
showNotification();
}
添加singleValueListenner,完成后,将haveNewItems设置为false。
js中的方法可以阅读here
答案 1 :(得分:0)
我设法弄明白了。如果你们中的一些人被困住了,这是一个很好的方法......
所以在你的活动中,创建一个类
public static boolean isBackgroundRunning(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
//If your app is the process in foreground, then it's not in running in background
return false;
}
}
}
}
return true;
}
感谢Mihir Patel为这件美丽的工作做好准备!
上面的代码将检查应用是否在后台或前台运行
然后只需在onChildAdded方法中添加该逻辑,例如:
mDatabasePosts = FirebaseDatabase.getInstance().getReference().child("posts");
mDatabasePosts.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if(isBackgroundRunning(MainActivity.this)){
showNotification();
}
}
@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) {
}
});
并且瞧,只有当您的应用在后台时,您才会收到通知。