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>