scb998
scb998

Reputation: 909

Error associated with receiving parse.com push notification Android

I have been trying to implement parse.com push notifications on my iOS and android app. Ive successfully been able to implement this on iOS, however, I'm still struggling with the Android implementation. When the notification is received by the user, and the user presses the notification, i want to open up my 'HomeScreen' activity, and display the text from the notification in an alert Dialogue. the Receiver handler and manifest files are shown below, along with the LogCat. What am i doing wrong, and how do i fix it?

HomeScreen Java:

public class Receiver extends ParsePushBroadcastReceiver {

    public Receiver(){

    }
    private static final String TAG = "MyNotificationsReceiver";

    @Override
    public void onPushOpen(Context context, Intent intent) {
        Log.e("Push", "Clicked");
        Intent i = new Intent(context, HomeScreen.class);
        i.putExtras(intent.getExtras());
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
            try {
                JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

                String notificationText = json.getString("alert");
                AlertDialog Alert= new AlertDialog.Builder(context)
                        .setTitle("News")
                        .setMessage(notificationText)
                        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){
                            public void onClick(DialogInterface dialog, int which){
                            }
                        })
                        .show();

            } catch (JSONException e) {
                Log.d(TAG, "JSONException: " + e.getMessage());

        }
    }

}

Manifest:

<service android:name="com.parse.PushService" />
        <receiver android:name="com.parse.ParseBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>
        <receiver android:name="com.parse.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <!--
                  IMPORTANT: Change "com.parse.tutorials.pushnotifications" to match your app's package name.
                -->
                <category android:name="au.gov.nsw.shellharbour.saferroadsshellharbour" />
            </intent-filter>
        </receiver>
        <receiver android:name="au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen$Receiver" android:exported="false">
            <intent-filter>
                <action android:name="com.parse.push.intent.RECEIVE" />
                <action android:name="com.parse.push.intent.DELETE" />
                <action android:name="com.parse.push.intent.OPEN" />
            </intent-filter>
        </receiver>

Logcat:

java.lang.RuntimeException: Unable to instantiate receiver au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen$Receiver: java.lang.InstantiationException: can't instantiate class au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen$Receiver; no empty constructor
            at android.app.ActivityThread.handleReceiver(ActivityThread.java:2265)
            at android.app.ActivityThread.access$1600(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4963)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.InstantiationException: can't instantiate class au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen$Receiver; no empty constructor
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1319)
            at android.app.ActivityThread.handleReceiver(ActivityThread.java:2260)
            at android.app.ActivityThread.access$1600(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4963)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 206

Answers (1)

JstnPwll
JstnPwll

Reputation: 8685

EDIT: Since Receiver is an inner class, it cannot exist outside of an instance of its outer class. This is why you cannot instantiate it via the manifest definition. To solve, mark Receiver as static, or define it as its own class file. (You can probably remove the empty constructor I had previously advised).


java.lang.InstantiationException: can't instantiate class au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen$Receiver; no empty constructor at android.app.ActivityThread.handleReceiver(ActivityThread.java:2265)

You need a constructor so the receiver can be instantiated:

public Receiver(){

}

Upvotes: 1

Related Questions