avivais / phonegap-parse-plugin

Phonegap 3.0.0 plugin for Parse.com push service
195 stars 315 forks source link

Android app crash on incoming notifications - com.parse.GcmBroadcastReceiver not found #31

Closed BarakChamo closed 7 years ago

BarakChamo commented 9 years ago

Hi there,

After installing the plugin incoming android notifications are captured but the app immediately crashes with the following error:

E/AndroidRuntime( 3709): java.lang.RuntimeException: Unable to instantiate receiver com.parse.GcmBroadcastReceiver: java.lang.ClassNotFoundException: Didn't find class "com.parse.GcmBroadcastReceiver" on path: DexPathList[[zip file "/data/app/co.yougoigo.app-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
E/AndroidRuntime( 3709):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2560)
E/AndroidRuntime( 3709):    at android.app.ActivityThread.access$1700(ActivityThread.java:144)
E/AndroidRuntime( 3709):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
E/AndroidRuntime( 3709):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 3709):    at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime( 3709):    at android.app.ActivityThread.main(ActivityThread.java:5221)
E/AndroidRuntime( 3709):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 3709):    at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime( 3709):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
E/AndroidRuntime( 3709):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
E/AndroidRuntime( 3709): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.parse.GcmBroadcastReceiver" on path: DexPathList[[zip file "/data/app/co.yougoigo.app-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
E/AndroidRuntime( 3709):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
E/AndroidRuntime( 3709):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
E/AndroidRuntime( 3709):    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
E/AndroidRuntime( 3709):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2555)
E/AndroidRuntime( 3709):    ... 9 more
E/AndroidRuntime( 3709):    Suppressed: java.lang.ClassNotFoundException: com.parse.GcmBroadcastReceiver
E/AndroidRuntime( 3709):        at java.lang.Class.classForName(Native Method)
E/AndroidRuntime( 3709):        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
E/AndroidRuntime( 3709):        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
E/AndroidRuntime( 3709):        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
E/AndroidRuntime( 3709):        ... 11 more
E/AndroidRuntime( 3709):    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

Anyone came across this error loading the receiver?

noman404 commented 9 years ago

Similar Problem Here

grrrian commented 9 years ago

Did you try the instructions in plugin.xml to allow notifications when the app is closed?

BarakChamo commented 9 years ago

I fixed this problem with some help on SO.

The problem, as described in the thread, is that Parse uses a different data structure for their Push service and assigns the message to the alert key with different handlers assigned to handle notifications. What I ended up doing is cloning PushPlugin and editing the problematic lines in place:

if (extras != null) {
  /*
    Original Interceptor
  */ 

  // // if we are in the foreground, just surface the payload, else post it to the statusbar
  // if (PushPlugin.isInForeground()) {
  //   extras.putBoolean("foreground", true);
  //   PushPlugin.sendExtras(extras);
  // } else {
  //   extras.putBoolean("foreground", false);

  //   // Send a notification if there is a message
  //   if (extras.getString("message") != null && extras.getString("message").length() != 0) {
  //     createNotification(context, extras);
  //   }
  // }

  /*
    Parse Fix
  */ 
  if (PushPlugin.isInForeground()) {

    extras.putBoolean("foreground", true);
    PushPlugin.sendExtras(extras);

  } else if (extras.getString("message") != null && !extras.getString("message").isEmpty()) {

    extras.putBoolean("foreground", false);
    createNotification(context, extras);

  } else {
    try{
      JSONObject data = new JSONObject(extras.getString("data"));
      // JSONObject data = payload.getJSONObject("data");
      String message = data.getString("alert"); //parse puts the message as an alert if you don't use custom json payload
      extras.putString("message", message);
      createNotification(context, extras);
    }
    catch (JSONException e)
    {
      Log.d(TAG, "Notification JSON Prase Error");
    }     
  }
}