Home Android 7,8 and Oxygen OS: Broadcast Receiver not working if app is killed
Reply: 0

Android 7,8 and Oxygen OS: Broadcast Receiver not working if app is killed

user4884
1#
user4884 Published in July 21, 2018, 1:50 pm

Post Android 7, my alarm receiver is not receiving the broadcast receiver if the app is killed by swiping it off from the recent tasks list.
Following is the piece from my AlarmReceiver, which extends BroadCastReceiver.

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(Constants.TAG, "event received");
        setContext(context);

        /* 
        *** My logic here *** 
        */

        Log.d(Constants.TAG, "alarm received");
    }
}

The place I am setting the alarm and registering the AlarmReceiver class for the alarm.

public class UnityNotificationManager
{
    public static void SetNotification(int id, long delayMs, String title, String message, String ticker, int sound, int vibrate,
                                       int lights, String largeIconResource, String smallIconResource, int bgColor, String bundle, String dataString)
    {
        Context currentActivity = UnityPlayer.currentActivity;
        AlarmManager am = (AlarmManager)currentActivity.getSystemService(Context.ALARM_SERVICE);

        Intent intent = new Intent(Constants.ALARM_RECEIVER);
        intent.setClass(currentActivity, AlarmReceiver.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);

        intent.putExtra("ticker", ticker);
        intent.putExtra("title", title);
        intent.putExtra("message", message);
        intent.putExtra("id", id);
        intent.putExtra("color", bgColor);
        intent.putExtra("sound", sound == 1);
        intent.putExtra("vibrate", vibrate == 1);
        intent.putExtra("lights", lights == 1);
        intent.putExtra("l_icon", largeIconResource);
        intent.putExtra("s_icon", smallIconResource);
        intent.putExtra("bundle", bundle);
        intent.putExtra("dataString", dataString);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(currentActivity,
                id, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        am.cancel(pendingIntent);

        long finalTime = System.currentTimeMillis() + delayMs;

        if (Build.VERSION.SDK_INT < 23) {
            am.set(AlarmManager.RTC_WAKEUP,finalTime, pendingIntent);
        } else {
            am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,finalTime, pendingIntent);
        }
        Log.d(Constants.TAG, "event fired finalTime = "+ finalTime);
    }
}

The value of the above used Constants.ALARM_RECEIVER is same as the action name defined in manifest for the broadcast AlarmReceiver will listen to.

<receiver android:name="com.example.app.AlarmReceiver">
      <intent-filter>
        <action android:name="com.example.app.alarm.action.trigger" />
      </intent-filter>
    </receiver>

The reason for using an explicit broadcast is that the android docs state that post Android Oreo, android applications won't be able to use implicit broadcasts in the background to improve performance. The link for the same: https://developer.android.com/about/versions/oreo/background.html#broadcasts

I also tried using a WakefulBroadCastReceiver to avoid optimizations made by Doze mode on android devices, but it turns out that this class has been deprecated in Android Oreo. The link for the same: https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html

The aforementioned code works for nearly every other manufacturer except One Plus devices, which run on oxygen os. Is there any way to fix this issue as notifications for applications like WhatsApp continue to work if app is killed the same way.

Additional Information:
1. I ran the command adb shell dumpsys package <package_name> | grep stopped on One Plus 5T and it returned the following output:

User 0: ceDataInode=2039857 installed=true hidden=false suspended=false stopped=false notLaunched=false enabled=0 instant=false

which, in turn suggests that the app is not stopped on swiping the app from the recent tasks list. Also, the "Force Stop" button is enabled on the application information page, suggesting that the app hasn't been force stopped.

  1. I was able to make the notifications work if I manually changed the Battery Optimizations setting from 'Optimized' to 'Not Optimized', but since notifications for other applications worked even when they are in the Optimized state, I don't believe this to be the solution for my problem.

Can someone please suggest any fixes or work-arounds to fix the notification issue on devices running Android 7 or above on Oxygen OS?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.314364 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO