android - Notifications every 24h at specified hour -


my code partially works. shows if app opened +/- 2h before time notification, later doesn't show notification. notification doesn't show @ time should.

public class backgroundservice extends service { private static final int first_notification = 9; private static final int second_notification = 17;   timer t1; timer t2;   @nullable @override public ibinder onbind(intent intent) {     return null; }  @override public void oncreate() {     super.oncreate();   }  @override public int onstartcommand(intent intent, int flags, int startid) {      super.onstartcommand(intent,flags,startid);     t1 =new timer();    // t2 = new timer();      log.d("service","service started!");     date da = new date();     da.settime(system.currenttimemillis());     if(da.gethours()>=first_notification){         da.setdate(da.getday()+1);     }     da.sethours(first_notification);     da.setminutes(0);     da.setseconds(0);     if(t1!=null) {         t1.schedule(new timertask() {             @override             public void run() {                 pushnotification();             }         }, da, 24 * 60 * 60 * 1000);     }     date d2 = new date();     d2.settime(system.currenttimemillis());     if(d2.gethours()>=second_notification){         d2.setdate(d2.getday()+1);     }     d2.sethours(second_notification);     d2.setminutes(0);     d2.setseconds(0);     if(t2!=null){         t2.schedule(new timertask() {             @override             public void run() {                 pushnotification();             }         },d2,24*60*60*1000);     }      return service.start_sticky; }    @override public void ondestroy() {     super.ondestroy();     log.d("service","service stoped!");  }    private void pushnotification(){     list<hashmap<string,string>> episodes = loadepisodes();     stringbuilder sb = new stringbuilder();     if(episodes.size()>0&&episodes!=null) {          (int = 0; < episodes.size(); i++) {             sb.append(episodes.get(i).get("serie") +" - "+episodes.get(i).get("name")+"\n");         }         string text = sb.tostring();          notificationcompat.builder builder = new notificationcompat.builder(this);         builder.setsmallicon(r.mipmap.ic_launcher);         builder.setcontenttitle("your new episodes!");         builder.setcontenttext(text);         builder.setautocancel(true);          intent intent = new intent(this,mainactivity.class);         intent.putextra("fragment",1);          pendingintent pintent = pendingintent.getactivity(this, 0, intent, 0);          builder.setcontentintent(pintent);          notification notification = builder.build();         notificationmanagercompat.from(this).notify(0, notification);     } } 

i doing changes , tried few options wasn't work different results. wan't never stop , shows everyday @ specified hours.

edit: tried make alarmmanager never shows notifications. mainactivity.java

alarmmanager = (alarmmanager)getapplicationcontext().getsystemservice(context.alarm_service);     intent inte = new intent(this,notificationreceiver.class);     pendingintent pi = pendingintent.getbroadcast(this,0,inte,0);     if(settings.getboolean("backgroundrunning",true)){        /*intent intent = new intent(this,backgroundservice.class);         bindservice(intent, m_serviceconnection,bind_auto_create);         startservice(intent);*/         calendar cal = calendar.getinstance();         cal.settimeinmillis(system.currenttimemillis());         cal.set(calendar.hour_of_day,17);         cal.set(calendar.minute,0);         cal.set(calendar.second,0);         am.setrepeating(alarmmanager.rtc_wakeup,cal.gettimeinmillis(),alarmmanager.interval_day,pi);      }     else if(am!=null&&settings.getboolean("backgroundrunning",true)){         am.cancel(pi);     } 


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -