我每次发布新公告时都会发布通知。我从Firebase获取通知,因此我在onChildAdded()
上调用通知功能。我收到了每个新公告的通知。但是,如果没有新的公告,即使在打开之后,最新公告也会偶尔重新出现。
通知类: 公共类FirebaseBackgroundService扩展了服务{
private Firebase f = new Firebase("https://infotrack.firebaseio.com/infotrack/announcements");
Query queryAnnouncements = f.orderByChild("timeStamp").limitToLast(1);
Firebase ref = new Firebase("https://infotrack.firebaseio.com/guests");
Firebase userRef = ref.child("users/"+ref.getAuth().getUid()+"/notifications");
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
ChildEventListener handler = new ChildEventListener() {
@Override
public void onChildAdded(final DataSnapshot dataSnapshot, String s) {
final AnnouncementsList announcement = dataSnapshot.getValue(AnnouncementsList.class);
Log.e("announcements", dataSnapshot.getKey());
userRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot notifSnapshot) {
Log.e("check", notifSnapshot.getValue().toString());
if (notifSnapshot != null) {
Notifications notif = notifSnapshot.getValue(Notifications.class);
if(!dataSnapshot.getKey().equals(notif.getAnnouncements())){
buildNotif("Announcement", announcement.getBody(), "New announcement from Infotrack");
userRef.child("announcements").setValue(dataSnapshot.getKey());
}
}
}
@Override
public void onCancelled(FirebaseError firebaseError) { }
});
}
@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(FirebaseError firebaseError) {}
};
queryAnnouncements.addChildEventListener(handler);
}
private void buildNotif(String body){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("New Announcement")
.setContentText(body)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setAutoCancel(true)
.setVibrate(new long[] { 1000, 500, 1000, 0, 1000 })
.setOnlyAlertOnce(true);
Intent intent = new Intent(this, Announcements.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent =
PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
builder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, builder.build());
}
}
广播接收器:
public class StartFirebaseAtBoot extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(FirebaseBackgroundService.class.getName()));
}
}
Android Manifest:
<service
android:name=".FirebaseBackgroundService"
android:exported="false"
android:process=":remote" >
</service>
<receiver android:name=".StartFirebaseAtBoot" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
这行我放在MainActivity的onCreate()方法中:
startService(new Intent(this, FirebaseBackgroundService.class));
堆栈跟踪:
02-14 18:41:25.262 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/announcements: -KAUMTXlrOFvFaj20TNi
02-14 18:41:25.282 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
02-14 18:41:25.372 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:25.382 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:25.802 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
02-14 18:41:25.812 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
02-14 18:41:25.902 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:25.902 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:25.942 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
02-14 18:41:25.942 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
02-14 18:41:25.962 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:25.972 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:25.992 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
02-14 18:41:25.992 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
02-14 18:41:26.012 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:26.032 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:26.042 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
02-14 18:41:26.052 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
02-14 18:41:26.122 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:26.132 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
02-14 18:41:26.152 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUM5H34QI-VklVRGSQ, news=1}
它一直持续到最后一行:
02-14 18:41:42.402 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
答案 0 :(得分:1)
当应用连接到服务器时,Firebase会为该位置的每个现有孩子拨打onChildAdded()
。因此,除非您删除子项,否则onChildAdded()
调用的数量将继续增加。
您有两种选择:
在您向用户显示通知后将其从数据库中删除
public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
AnnouncementsList announcement = snapshot.getValue(AnnouncementsList.class);
buildNotif(announcement.getBody());
snapshot.getRef().remove();
}
标记用户已看到的通知并跳过
大多数应用程序都使用第一个选项,因为它会自动缩短队列。