0

I'm trying to send a notification at a particular time, this part works perfectly but after I reboot the phone, the service doesn't get started unless the app is opened which then starts the service. I've tried multiple solutions online but still can't fix the issue.

Activity:

private void startNotificationAlarm() {

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, ReminderBroadcastReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), intent, 0);
    Objects.requireNonNull(alarmManager).setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

BroadcastReceiver:

public class ReminderBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        Intent serviceIntent = new Intent(context, BootService.class);
        intent.setAction("<package>.Receiver");
        context.startService(serviceIntent);

    } else {

        scheduleNotification(context, intent);
    }
}

private void scheduleNotification(Context context, Intent intent) {

    Notification notification = new NotificationCompat.Builder(context, BaseApp.CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_calendar_alert)
            .setContentTitle(title)
            .setContentText(message)
            .setColor(color)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_REMINDER)
            .setAutoCancel(true)
            .setOnlyAlertOnce(false)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .build();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Objects.requireNonNull(notificationManager).notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), notification);
}

IntentService:

public class BootService extends IntentService {

public BootService() {
    super("BootService");
}

@Override
protected void onHandleIntent(Intent intent) {
    if (intent != null) {
        try {

            Thread.sleep(5000);
            startNotification();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
  }
}

Manifest

<receiver
        android:name=".Receiver.ReminderBroadcastReceiver" android:enabled="true" android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>

    <service android:name=".Service.BootService"/>
tony
  • 466
  • 6
  • 22

1 Answers1

0

You have to provide below permission in manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

Also you have to make android:exported as true as per android docs:

android:exported

Whether or not the broadcast receiver can receive messages from sources outside its application — "true" if it can, and "false" if not. If "false", the only messages the broadcast receiver can receive are those sent by components of the same application or applications with the same user ID.

sample code

<receiver android:name=".sample.BootReceiver"
          android:enabled="true"
          android:exported="true">
     <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
     </intent-filter>
</receiver>
Ashok Kumar
  • 1,226
  • 1
  • 10
  • 14
  • I have that permission provided in my manifest. I only forgot to include it in the question. – tony Aug 26 '19 at 13:56
  • For me ,on phone reboot I gets the broadcast and able to see the toast msg on screen that I had added in broadcast receiver. – Ashok Kumar Aug 26 '19 at 14:01
  • I'll try other phones and see if the issue still persists – tony Aug 26 '19 at 14:32
  • I changed to `android:exported=true` but unfortunately it still doesn't work. Thanks for your help though. – tony Aug 26 '19 at 15:39
  • did you try to show toast from onReceive() method ? just to confirm if above setup works for you. – Ashok Kumar Aug 26 '19 at 17:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/198502/discussion-between-tony-and-ashok-kumar). – tony Aug 26 '19 at 20:19