Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by ibrahim23 · Jun 17, 2014 at 03:06 PM · pluginandroidpluginjni

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>
 


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by hirenkacha · Sep 30, 2014 at 11:04 AM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image erosales · Apr 08, 2015 at 03:48 AM 0
Share

Hi @hirenkacha,

Did you get this working on Android 4.1 and up? I seem to be having a problem getting this to work, any advice?

avatar image
0

Answer by liortal · Aug 03, 2014 at 10:38 PM

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).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Using AndroidJavaObject.CallStatic to retrieve a return value 0 Answers

Now that Unity 4.1.2 broke the Android plugin examples, what do you use to learn them? 1 Answer

Callback from native Java to Unity's C# script has some weird (latency?) issue 0 Answers

Getting byte[] or ByteBuffer[] from native Java 1 Answer

in callJavaCode JNI example, question about JNI_OnLoad 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges