在指定时间每24小时发出通知

时间:2016-05-28 17:18:44

标签: android notifications

我的代码部分有效。它显示应用程序是否在通知时间之前+/- 2小时打开,之后它不会显示任何通知。有时,通知不会在时间显示。

{
  "page": {
    "page": 1,
    "totalPages": 1,
    "pageSize": 2,
    "total": 2
  },
  "thermostatList": [
    {
      "identifier": "12345",
      "name": "Downstairs",
      "thermostatRev": "160528163253",
      "isRegistered": true,
      "modelNumber": "athenaSmart",
      "brand": "ecobee",
      "features": "",
      "lastModified": "2016-05-28 16:32:53",
      "thermostatTime": "2016-05-28 11:57:00",
      "utcTime": "2016-05-28 16:57:00",
      "runtime": {
        "runtimeRev": "160528165546",
        "connected": true,
        "firstConnected": "2015-08-20 21:57:53",
        "connectDateTime": "2016-05-26 08:56:18",
        "disconnectDateTime": "2016-05-26 00:00:00",
        "lastModified": "2016-05-28 16:55:46",
        "lastStatusModified": "2016-05-28 16:55:46",
        "runtimeDate": "2016-05-28",
        "runtimeInterval": 200,
        "actualTemperature": 703,
        "actualHumidity": 49,
        "desiredHeat": 670,
        "desiredCool": 810,
        "desiredHumidity": 44,
        "desiredDehumidity": 56,
        "desiredFanMode": "on"
      }
    },
    {
      "identifier": "310166836750",
      "name": "Upstairs",
      "thermostatRev": "160528162656",
      "isRegistered": true,
      "modelNumber": "athenaSmart",
      "brand": "ecobee",
      "features": "",
      "lastModified": "2016-05-28 16:26:56",
      "thermostatTime": "2016-05-28 11:57:00",
      "utcTime": "2016-05-28 16:57:00",
      "runtime": {
        "runtimeRev": "160528165523",
        "connected": true,
        "firstConnected": "2015-09-28 13:33:41",
        "connectDateTime": "2016-05-26 08:55:35",
        "disconnectDateTime": "2016-05-26 00:00:00",
        "lastModified": "2016-05-28 16:55:23",
        "lastStatusModified": "2016-05-28 16:55:23",
        "runtimeDate": "2016-05-28",
        "runtimeInterval": 201,
        "actualTemperature": 712,
        "actualHumidity": 49,
        "desiredHeat": 600,
        "desiredCool": 840,
        "desiredHumidity": 36,
        "desiredDehumidity": 60,
        "desiredFanMode": "auto"
      }
    }
  ],
  "status": {
    "code": 0,
    "message": ""
  }
}

我正在做一些更改并尝试了一些选项,但它不能用于不同的结果。 我不想在指定的时间每天停下来表演。

编辑: 我尝试用AlarmManager制作它,但它从不显示通知。 MainActivity.java

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) {

        for (int i = 0; i < 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);
    }
}

0 个答案:

没有答案