Hy guys. I’ve implemented Google Cloud Messaging in my app,but i have a problem when i try to display the message received from the server to the user.
I’ve managed to display the message in a toast,but i would like to use an Alert Dialog.
This is my GCMBaseIntentService:
public class GCMIntentService extends GCMBaseIntentService {
public static final String SENDER_ID = "546677596418";
public GCMIntentService() {
super(SENDER_ID);
}
@Override
protected void onError(Context arg0, String arg1) {
// TODO Auto-generated method stub
Log.e("Registration", "Got an error!");
Log.e("Registration", arg0.toString() + arg1.toString());
}
@Override
protected void onMessage(final Context arg0, final Intent arg1) {
// TODO Auto-generated method stub
Log.i("Registration", "Got a message!");
Log.i("Registration", arg1.getStringExtra("message"));
Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable(){
public void run() {
// TODO Auto-generated method stub
Toast.makeText(arg0, arg1.getStringExtra("message"), Toast.LENGTH_LONG).show();
/* String msg = arg1.getStringExtra("message");
if(msg != null){
new AlertDialog.Builder(arg0)
.setTitle("New Notification")
.setMessage(msg)
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create().show();
}
*/
}
});
}
}
the commented part is my attempt to display an alert Dialog.I think the context is the problem,but i don’t really know how to fix it.
It would be better if i use notifications for this?
i’ve solved the problem using notifications.I guess this will have to do:)