0

I want to show local notification on specific time. So that I am using alarm manager to set pending intent for specific time. But in my case Broadcast/Service is not getting called if application killed by user.

Check the below code and help me out why am not getting notification after application killed.

public class MainActivity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        

        Intent notifyIntent = new Intent(this,MyReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast
                (MainActivity.this, 1, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,  System.currentTimeMillis()+30000, pendingIntent);
    }
}
public class MyReceiver extends BroadcastReceiver {

    public MyReceiver() {
        
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        
  
        Intent intent1 = new Intent(context, MyNewIntentService.class);
        context.startService(intent1);
    }
}
public class MyNewIntentService extends Service {

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        CommonUtil.showNotification(getApplicationContext());
        return START_STICKY;
    }
}

AndroidManifest.xml


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

  <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="false"
            />

        <service
            android:name=".MyNewIntentService"
            android:exported="false"
            />
Maradiya Krupa
  • 213
  • 1
  • 6
  • Did you solve this problem? I am also facing the same issue here. If you solved it please share the solution with us. TIA – fahad_sust Oct 24 '20 at 15:46

1 Answers1

0

You can check here for working sample of Alarm with Broadcast receiver.

How to use Android AlarmManager in Fragment in Kotlin?

Vitaliy-T
  • 733
  • 6
  • 23
  • Thank you for your quick response but this is not working for me after app got killed by user in android 10. – Maradiya Krupa Oct 09 '20 at 09:40
  • Are you using Emulator? If so, what phone? Also, try without the `Intent Service` that you're using. Ideally, try to completely replace your code with the solution I posted and see if it works. If it doesn't, problem is something else. – Vitaliy-T Oct 09 '20 at 09:43