I am extracting the text of a push notification and display that in an alert - however, I'm getting a Fatal exception when the receiver is being activated (see the logcat below)
11-26 08:35:51.919 4607-4607/au.gov.nsw.shellharbour.saferroadsshellharbour E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start receiver au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen$Receiver: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2291)
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: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:716)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:356)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:234)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:153)
at android.app.Dialog.show(Dialog.java:277)
at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
at au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen$Receiver.onPushOpen(HomeScreen.java:198)
at com.parse.ParsePushBroadcastReceiver.onReceive(ParsePushBroadcastReceiver.java:108)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2284)
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)
11-26 08:36:06.779 4607-4607/au.gov.nsw.shellharbour.saferroadsshellharbour I/Process﹕ Sending signal. PID: 4607 SIG: 9
Below is my Receiver class:
private void displayAlert(){
AlertDialog Alert= new AlertDialog.Builder(this)
.setTitle("News")
.setMessage(notificationText)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
}
})
.show();
}
public static class Receiver extends ParsePushBroadcastReceiver {
private String notificationText;
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"));
notificationText = json.getString("alert");
} catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
}
}
}
UPDATE: so now I'm able to open the activity no problem, but cannot extract the push notification message, place it in the alert dialogue, and display the alert dialogue.