firebase - Android Notification re-appears after being opened -


i posting notifications everytime new announcement posted. fetch announcements firebase call notification function on onchildadded(). i'm receiving notifications every new announcement. however, if there isn't new announcement, latest announcement re-appears every once in while 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 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 placed in oncreate() method of 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 last line:

02-14 18:41:42.402 2446-2446/xyz.kimfeliciano.infotrack.infotrack:remote e/check: {announcements=-kaumtxlrofvfaj20tni, news=1} 

when app connects server, firebase call onchildadded() each existing child @ location. unless delete children, number of onchildadded() invocations keep going up.

you have 2 options:

  • remove notification database after you've shown user

    public void onchildadded(datasnapshot snapshot, string previouschildkey) {     announcementslist announcement = snapshot.getvalue(announcementslist.class);     buildnotif(announcement.getbody());     snapshot.getref().remove(); } 
  • mark notifications user has seen , skip those

most applications go first option, since automatically keeps queue short.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -