Search code examples
androidkotlinbroadcastreceiverandroid-pendingintent

Broadcast onReceive method does not get called


DUE TO COMMENTS, CODE IS UPDATED WITH MORE SPECIFIC INFORMATION.

Manifest.xml

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

        <intent-filter>
            <action android:name="com.x.x.x.NOTIFICATION_INTENT_ACTION.TEST" />
        </intent-filter>
</receiver>

MyReceiver.java

internal class MyReceiver: BroadcastReceiver() {

    companion object {
        const val NOT_ID = ".."
        const val NOT = ".."
        const val NOTIFICATION_INTENT_ACTION = "com.x.x.x.NOTIFICATION_INTENT_ACTION.TEST"

    }

    override fun onReceive(context: Context, intent: Intent) {
        // does not get called
    }
}

CreatingIntent.java

    Intent intent = new Intent(this, MyReceiver.class);
    mPendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    mAlarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    if (mAlarmManager != null) {
        mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 60000L, mPendingIntent);
    }

Problem: onReceive method inside of NotRec class does not get called. I tried to change Manifest to <receiver android:name="FULL_PATH.receiver.NotRec" /> but it didn't change anything. Any ideas?


Solution

  • Remove

    android:exported="false"
    

    from the <receiver> declaration in the manifest.

    If you don't "export" your receiver, the AlarmManager cannot trigger it. When you mark a component in the manifest as "not exported", this means that it is "private" and other applications (including system applications like AlarmManager) cannot launch or trigger the component.