11

I have a service that schedules a pendingintent which starts my notification. However, since Android O I am getting this error. I did some research, and stumbled upon context.registerReceiver , but that does not seem to fix the problem.

Error:

W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:my.great.package flg=0x4000010 (has extras) } to com.google.android.googlequicksearchbox/com.google.android.apps.gsa.googlequicksearchbox.GelStubAppWatcher

```

My pendingintent:

Intent myNotification = new Intent("services.notifications.Notification");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, (int) (Math.random() * Integer.MAX_VALUE), myNotification, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));
alarmManager.setExact(AlarmManager.RTC_WAKEUP, day.getTimeInMillis(), pendingIntent);

My notification:

public class Notification extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        context.registerReceiver(this, new IntentFilter());

        try {
            WakeLock wakeLock = ((PowerManager) context.getSystemService(Context.POWER_SERVICE)).newWakeLock(1, "NotificationWakeLock");
            wakeLock.acquire(10000);

            try {
                scheduleNotification(context, intent);
            } finally {
                wakeLock.release();
            }
        } catch (NullPointerException e) {}
    }
}
2
  • Solved it, will add solution later
    – Jason
    Commented Sep 26, 2017 at 19:04
  • how did you achieve this?
    – NinjaCoder
    Commented Nov 13, 2017 at 17:53

1 Answer 1

0

I resolved it by adding a foreground service:

Intent test = new Intent(this, NotificationService.class);
startForegroundService(test);

This will show a notification telling that my app is running on the foreground.

And by adding this in my service's oncreate:

startForeground(100, new NotificationCompat.Builder(this).build());
4
  • 2
    Not useful for me, onReceive method is not even getting called
    – fillobotto
    Commented Apr 26, 2018 at 10:27
  • Any other solution except dispalying notification all time to the user, which must be not a good idea? Commented Jul 13, 2018 at 11:19
  • @AnkitKumarSingh Please see this answer: stackoverflow.com/a/54273840/297710 - it is about self-registering to the needed implicit intents. And mentions, when foreground service may be needed...
    – yvolk
    Commented Jan 20, 2019 at 5:48
  • What did that have to do with googlequicksearchbox? Commented Oct 6, 2020 at 15:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.