У меня есть рабочая реализация C2DM, если я обрабатываю каждое полученное сообщение с помощью Toast.
Вместо этого я хотел бы использовать диалоговое окно Alert, которое пользователь говорит «ОК», чтобы подтвердитьmessage.
Код, который я написал для этого, генерирует исключение в строке show ().Я ожидаю, что проблема может быть связана с используемым контекстом (?).
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();
Полный класс (как запрошено в комментарии):
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();*/
}
}