Reputation: 117
I am posting notifications everytime a new announcement is posted. I fetch announcements from Firebase so I call the notification function on onChildAdded()
. I'm receiving notifications for every new announcement. However, if there isn't a new announcement, the latest announcement re-appears every once in a while even after opening it.
Notifications class: public class FirebaseBackgroundService extends Service {
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());
}
}
Broadcast Receiver:
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>
This line I placed in the onCreate() method of my MainActivity:
startService(new Intent(this, FirebaseBackgroundService.class));
Stacktrace:
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}
it goes on until this last line:
02-14 18:41:42.402 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote E/check: {announcements=-KAUMTXlrOFvFaj20TNi, news=1}
Upvotes: 1
Views: 209
Reputation: 600100
When the app connects to the server, Firebase will call onChildAdded()
for each existing child at the location. So unless you delete the children, the number of onChildAdded()
invocations will keep going up.
You have two options:
remove the notification from the database after you've shown it to the user
public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
AnnouncementsList announcement = snapshot.getValue(AnnouncementsList.class);
buildNotif(announcement.getBody());
snapshot.getRef().remove();
}
mark the notifications that the user has seen and skip those
Most applications go with the first option, since it automatically keeps the queue short.
Upvotes: 1