Note that there are some explanatory texts on larger screens.

plurals
  1. POC2DM Show AlertDialog on Receive
    primarykey
    data
    text
    <p>I have a working implementation of C2DM, if I handle each message I receive with a Toast.</p> <p>Instead, I would like to use an Alert Dialog, which the user says OK to so as to acknowledge the message.</p> <p>The code I've written for this generates an exception at the show() line. I expect the issue may be to do with the context used (?).</p> <pre><code>AlertDialog.Builder alertbox = new AlertDialog.Builder(context); alertbox.setMessage("Received Push Notification"); alertbox.setNeutralButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }); AlertDialog alert = alertbox.create(); alert.show(); </code></pre> <p>Full Class (as requested in comment):</p> <pre><code>public class MyC2DM extends BroadcastReceiver { public MyC2DM() { } @Override public void onReceive(Context context, Intent intent) { Log.i("Recieve","2"); //Toast.makeText(context, "Intent Receive!", Toast.LENGTH_SHORT).show(); if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) { handleRegistration(context, intent); } else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) { handleMessage(context, intent); } } private void handleRegistration(Context context, Intent intent) { String registration = intent.getStringExtra("registration_id"); if (intent.getStringExtra("error") != null) { Toast.makeText(context, "Reg Error!", Toast.LENGTH_LONG).show(); // Registration failed, should try again later. } else if (intent.getStringExtra("unregistered") != null) { // unregistration done, new messages from the authorized sender will be rejected Toast.makeText(context, "Unreg!", Toast.LENGTH_LONG).show(); } else if (registration != null) { // Send the registration ID to the 3rd party site that is sending the messages. // This should be done in a separate thread. // When done, remember that all registration is done. // UserId = customer.getId(); // Log.i("id",String.valueOf(UserId)); String RegId = registration; Log.i("reg",String.valueOf(RegId) ); } } private void handleMessage(Context context, Intent intent) { // Message handler. Log.i("Recieve","MESSAGE"); AlertDialog.Builder alertbox = new AlertDialog.Builder(context); alertbox.setMessage("Received Push Notification"); alertbox.setNeutralButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }); AlertDialog alert = alertbox.create(); alert.show(); /*Toast.makeText(context, "ALERT: " + intent.getStringExtra("payload"), Toast.LENGTH_LONG).show();*/ } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload