BroadcastReceiver onReceive not called from custom Unity android plugin

I’m writing my own Unity plugin for firing timed local notifications on Android. I’ve managed to make the necessary JNI calls through Unity but for some reason the onReceive method of my BroadcastReceiver is not called. Below is the code for classes included in my plugin. The scheduleNotification method is called from a C# script but there is no notification after the time specified by ‘interval’ has passed.

The plugin is added as a jar file in Unity (placed at Assets/Plugins/Android/). I also have a res folder at this path which gives me the icons to be used with the notification. What am I doing wrong here? Why is my BroadcastReceiver not called?

PushPlugin.java

public class PushPlugin extends UnityPlayerActivity
{
    private static final String TAG = "PushPlugin_Unity";
    private static PushPlugin instance = null;

    @Override
    protected void onCreate(Bundle myBundle) {
        instance = this;
        super.onCreate(myBundle);
    }

    public static void scheduleNotification(String notificationID,
                                        String message, String objectId, int interval) {

        // get a Calendar object with current time
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, interval);

        Intent intent = new Intent(UnityPlayer.currentActivity.getApplicationContext(), PluginBroadcastReceiver.class);
        intent.putExtra("notificationID", notificationID);
        intent.putExtra("message", message);
        intent.putExtra("objectId", objectId);
    
        PendingIntent sender = PendingIntent.getBroadcast(UnityPlayer.currentActivity.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
        // Get the AlarmManager service
        AlarmManager am = (AlarmManager) UnityPlayer.currentActivity.getApplicationContext().getSystemService(UnityPlayer.currentActivity.getApplicationContext().ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
   
    }
}

PluginBroadcastReceiver.java

public class PluginBroadcastReceiver extends BroadcastReceiver {
public static Bundle extras = new Bundle();
static final String TAG = "PushPlugin-BR";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
Context ctx;

@Override
public void onReceive(Context context, Intent intent) {
	ctx = context;
    
	// Local notification.
	Bundle bundle = intent.getExtras();
	String notificationID = bundle.getString("notificationID");
	String message = bundle.getString("message");
	String objectId = bundle.getString("objectId");

	Log.v(TAG, "Received local notification of type: " + notificationID
			+ " and message: " + message + " and objectId " + objectId);

	sendNotification(message, notificationID, objectId);
}

// Put the GCM message into a notification and post it.
private void sendNotification(String msg, String notifID, String objectID) {
	mNotificationManager = (NotificationManager) ctx
			.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(ctx, PushPlugin.class);
    
	PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
			intent, PendingIntent.FLAG_UPDATE_CURRENT);

    extras.putString("notificationID", notifID);
    extras.putString("message", msg);
    extras.putString("objectId", objectID);
    
	NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
			ctx);
    
    Resources res = ctx.getResources();
    int icon = res.getIdentifier("app_icon", "drawable", UnityPlayer.currentActivity.getPackageName());
    
    mBuilder.setSmallIcon(icon);
	mBuilder.setContentTitle("Plugin Test");
	Log.v(TAG, "-------- notification received: " + msg);
	mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg));
	mBuilder.setContentText(msg);
	mBuilder.setContentIntent(contentIntent);
	mBuilder.setAutoCancel(true);

	mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.app"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

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

<!-- gcm -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<permission android:name="com.test.app.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.test.app.permission.C2D_MESSAGE" />

<application android:label="@string/app_name">
    <activity android:name=".PushPlugin"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    
    <receiver
        android:name=".PluginBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.test.app" />
        </intent-filter>
    </receiver>
    <service android:name=".PluginIntentService" />
</application>

Ok… I found the solution for this. Just declare the BroadCastReceiver in BOTH the AndroidManifest.xml ie in Unity and in your JAR file as a receiver.

<receiver android:name="com.test.app.PluginBroadcastReceiver">

as said by @liortal.

Not sure if that may solve the issue, but in your AndroidManifest.xml, try declaring the BroadcastReceiver using its full name:

<receiver android:name="com.test.app.PluginBroadcastReceiver">

I remember having issues with having it the way you shoe in your question.

Also, for the way you’re calling the broadcast receiver in your example, you do not need to declare anything but its full name in the manifest (you can keep only the part as i show above).