I have working in background task when I kill my application keep on check background task.
I want when Kill my application keep Every checking data from API any new data is there it's show local push notification. This code is working marshmallow and below version but Nougat and Above version not working can you help me.
public class MyService extends Service {
public static final int notify = 60*10*1000; //interval between two services(Here Service run every 5 Minute)
private Handler mHandler = new Handler();
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
if (mTimer != null) // Cancel if already existed
mTimer.cancel();
else
mTimer = new Timer(); //recreate new
mTimer.scheduleAtFixedRate(new TimeDisplay(), 0, notify); //Schedule task
//mTimer.scheduleAtFixedRate(new MyTask(), millisToNextHour(calendar), 60*60*1000, TimeUnit.MILLISECONDS);
}
@TargetApi(Build.VERSION_CODES.O)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void processFinish(String output) {
if (count20new > count20old) {
// Toast.makeText(MyService.this, "Service is running", Toast.LENGTH_SHORT).show();
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notificationIntent);
String message = "To see the latest message.";
Intent intent = new Intent(getApplicationContext(), MyService.class);
// PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(getApplicationContext());
Resources res = getApplicationContext().getResources();
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_stat_final)
.setColor(Color.parseColor("#83c3ed"))
.setTicker("Check Latest Notification")
.setContentTitle(res.getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent);
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
}
}
public int getProfilesCount() {
Log.e("my_state","On start command6");
Cursor cursor = db.rawQuery(countQuery, null);
count20new = cursor.getCount();
cursor.close();
return count20new;
}
//class TimeDisplay for handling task
class TimeDisplay extends TimerTask {
@Override
public void run() {
mHandler.post(new Runnable() {
@Override
public void run() {
boolean isConnected = ConnectivityReceiver.isConnected(MyService.this);
if (isConnected) {
Notificationdata();
}
}
});
}
}
public void Notificationdata(){
Log.e("my_state","On start command8");
}
}