android - Firebase onMessageReceived is triggering twice -


i scheduling notification launched when message received firebase service problem notification triggering twice(once on time , second time after maybe 30 min) here code:

 sharedpreferences = preferencemanager             .getdefaultsharedpreferences(getapplicationcontext());     // check if message contains data payload.     if (remotemessage.getdata().size() > 0) {         intent myintent = new intent(myfirebasemessagingservice.this, myalarmservice.class);         pendingintent = pendingintent.getservice(myfirebasemessagingservice.this, 0, myintent, 0);         alarmmanager alarmmanager = (alarmmanager)getsystemservice(alarm_service);         long currenttime = system.currenttimemillis();          int hour=sharedpreferences.getint("hour",9);         int min=sharedpreferences.getint("min",0);         calendar calendar =  calendar.getinstance();         calendar.set(calendar.hour_of_day,hour);         calendar.set(calendar.minute,min);            alarmmanager.set(alarmmanager.rtc_wakeup,calendar.gettimeinmillis() , pendingintent); 

this myalarmservice onstartcommand:

 public int onstartcommand(intent intent, int flags, int startid) {           uri uri = ringtonemanager.getdefaulturi(ringtonemanager.type_notification);         notificationcompat.builder mbuilder = new notificationcompat.builder(this);         mbuilder.setsmallicon(r.mipmap.ic_launcher);         mbuilder.setcontenttitle("figures");         mbuilder.setcontenttext("new figures has been updated!");         mbuilder.setautocancel(true);         mbuilder.setsound(uri);         intent resultintent = new intent(this, loginactivity.class);         taskstackbuilder stackbuilder = taskstackbuilder.create(this);         stackbuilder.addparentstack(loginactivity.class);         stackbuilder.addnextintent(resultintent);         pendingintent resultpendingintent = stackbuilder.getpendingintent(0, pendingintent.flag_update_current);         mbuilder.setcontentintent(resultpendingintent);         notificationmanager mnotificationmanager = (notificationmanager) getsystemservice(context.notification_service);         mnotificationmanager.notify(0, mbuilder.build());       return super.onstartcommand(intent, flags, startid); } 

and here vb.net sending notification function:

private function sendnotification() integer     try         dim url string = "https://fcm.googleapis.com/fcm/send"         dim trequest webrequest = webrequest.create(url)          trequest.method = "post"         trequest.contenttype = "application/json"         dim data = new { _             key .[to] =mytopic, _                 key .ttl = "109", _             key .data = new { _                 key .body = "updated", _                 key .title = "daily" _             } _         }         dim jssons string = newtonsoft.json.jsonconvert.serializeobject(data)         dim bytearray byte() = encoding.utf8.getbytes(jssons)         trequest.headers.add(string.format("authorization: key={0}", mykey))         trequest.headers.add(string.format("sender: id={0}", mysenderid))         trequest.contentlength = bytearray.length         trequest.contenttype = "application/json"         using datastream stream = trequest.getrequeststream()             datastream.write(bytearray, 0, bytearray.length)              using tresponse webresponse = trequest.getresponse                  using datastreamresponse stream = tresponse.getresponsestream()                      using treader streamreader = new streamreader(datastreamresponse)                          dim sresponsefromserver string = treader.readtoend()                          log(sresponsefromserver, w)                     end using                 end using              end using         end using      catch ex webexception         log(ex.message, w)         return ex.status        end try     log("notification sent", w)     return 200  end function  

is there better way send notification?, there problem within alarmmanager? thank you.

return start_not_sticky myalarmservice.onstartcommand() instead of calling super method. super method returns start_sticky, restart service after gets killed system, re-triggering notification.

the other question whether using service idea in general. i'd try broadcastreceiver, won't keep running after you're done.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -