Sahil Shokeen
Sahil Shokeen

Reputation: 336

Service code not running after application swiped from recent apps

Hi i am using IntentService to show notifications to user based on their time set which is stored in notificationCalendar variable. I start service using AlarmManager and when app is in background(Pressed home button) or running i receive notification but when app is swiped from recent apps no notification is received.

I searched for solutions online for not allowing service to destroy so i return START_STICKY in service onStartCaommand and in manifest i have added stopWithTask=false.

I am testing it on MI phone with API level 22.

Here's my code:

private void sendNotification(){
    Intent intent = new Intent(this, NotificationService.class);
    PendingIntent alarmIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, notificationCalendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, alarmIntent);
}

Service class to show notifications:

public class NotificationService extends Service {

private static final int NOTIFICATION_ID = 1;

public NotificationService() {
    super();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    processStartNotification();
    return START_STICKY;
}

@Override 
public IBinder onBind(Intent intent) {
    return null;
} 

private void processStartNotification() {
    // Do something. For example, fetch fresh data from backend to create a rich notification?
    NotificationManager notificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Habit Time")
            .setContentText("Hey time for your habit")
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setSound(alarmSound)
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});

    notificationManager.notify(NOTIFICATION_ID, mNotifyBuilder.build());
}

@Override
public void onTaskRemoved(Intent rootIntent){
    Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
    restartServiceIntent.setPackage(getPackageName());

    PendingIntent restartServicePendingIntent =  PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(
            AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime() + 1,
            restartServicePendingIntent);

    super.onTaskRemoved(rootIntent);
  }
}

//Manifest file

<service
        android:name=".notification.NotificationService"
        android:enabled="true"
        android:stopWithTask="false"/>

Upvotes: 1

Views: 272

Answers (1)

David Wasser
David Wasser

Reputation: 95578

On some phones, you need to add your app to the list of apps allowed to run in the background. Some Huawei, LG and Xiaomi phones have this. If your app is not in this list, swiping the task from the list of recent tasks will kill any background processes and not restart them.

Look in Settings->Battery->Manage Apps Battery Usage

Upvotes: 1

Related Questions