0

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.

scb998
  • 899
  • 5
  • 18
  • 42
  • are you trying to call an alertDialog from a receiver? that's a NO! NO! NO!.. rather transfer your alerDialog code to your activity class being the HomeScreen.class.. okay Sir? – Elltz Nov 25 '14 at 21:52
  • How then do i trigger the alert when the notification is received? – scb998 Nov 25 '14 at 21:57
  • take a look at this sloution: http://stackoverflow.com/a/8766864/4224337 – Rami Nov 25 '14 at 21:58
  • taking the link @Rami gave into consideration, you could apply that and also create a static boolean variable and its false by default and anytime a push or notification is received, you set it to true and check for for it in your activity onresume and after you show set to false.. i am legit-ly lucid, Sir? – Elltz Nov 25 '14 at 22:02

3 Answers3

1

The Context that is given to a BroadcastReceiver is not able to show a Dialog.

It sounds like you should instead follow the standard pattern of displaying a Notification using the NotificationManager. That's the more standard experience and what your users would expect.

The following link will take to to the documentation for a Notifications example and the full page is worth a read to understand the different options available.

http://developer.android.com/guide/topics/ui/notifiers/notifications.html#SimpleNotification

Ross Hambrick
  • 5,880
  • 2
  • 43
  • 34
1
  public class scb998 extends Application { // this could be any singleton class not just application
    static boolean scb988b; // its false by default, but feel free to instatiate it
    static String msg = ""; // this came after the edit
   }

then in your receiver class right after this context.startActivity(i); trigger your boolean to true.. then follow my comments... actually that's it.. also you could pass the string to the activity via the intent function putString or a static singleton String variable anyway Sir..

actually this just for reps...lol..

EDIT AFTER YOUR LAST COMMENT

the whole story is you are trying to show a dialog, when a msg is received-but you can't do it in your receiver, so all you do is communicate with your activity when is up to let it know that a new msg has been received, there all so many ways to do it, starting from otto, to local broadcast manager , you can learn these on your own, or use this simple way of mine, that extends the application class, and put a static boolean like i did.. don not put it in Receiver you could also create a static string for your noty message Scb998.msg = notificationText = json.getString("alert");, then after context.startActivity(i); set your boolean Scb998.scb988b = true;, after then remove and put it in your activity rather

 void calalert(){
       AlertDialog Alert= new AlertDialog.Builder(this)
        .setTitle("News")
        .setMessage(Scb998.msg)
        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
            }
        })
        .show();
        Scb998.scb988b = false;
 }

now call it in your Home class onresume and check for the boolean

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    if(Scb998.scb988b)
        calalert();
}
Community
  • 1
  • 1
Elltz
  • 10,730
  • 4
  • 31
  • 59
  • Apparently the boolean cannot be declared as static - does that change anything? – scb998 Nov 25 '14 at 22:17
  • i tried to implement this in Android studio, and i get a warning stating that inner classes cannot have static declaration. – scb998 Nov 25 '14 at 22:23
  • [check this ok](http://stackoverflow.com/questions/1953530/why-does-java-prohibit-static-fields-in-inner-classes). so create a new class ok, Sir? or extend the application class like i did and declare it your manifest too- that's if you extended the application.. clear now? – Elltz Nov 25 '14 at 22:31
  • not particularly. sorry, I'm quite new to android (everything seems so much easier on iOS). see my updated question. I don't know how to extract the alert text from the statically declared receiver, put that in the message section of the alert dialogue, and then display the alert dialogue. – scb998 Nov 25 '14 at 22:42
  • lol, actually my answer just covered all, but since you say you new, you probably need codes so [this](http://stackoverflow.com/questions/5265913/how-to-use-putextra-and-getextra-for-string-data) .. so after you do the putting and getting you display it..and showing it, is just following the way you doing it ok, that all@scb998 – Elltz Nov 25 '14 at 22:50
  • Im really not getting this at all. Ill mark your answer as correct, as it has removed the error and ask a new question. – scb998 Nov 25 '14 at 23:44
  • IT WORKS!!!!! thank you so so so so much! its great to find a person on stack that is willing to stick with a problem through to completion! – scb998 Nov 27 '14 at 21:54
0

Just use YOUR-ACTIVITY-NAME.this for alert builder context.

Corrected form, for example:

 AlertDialog Alert= new AlertDialog.Builder(YOUR-ACTIVITY-NAME.this)
Shayan Amani
  • 5,787
  • 1
  • 39
  • 40