1
Hy guys。我在我的應用中實施了Google雲消息傳遞,但是當我嘗試將從服務器接收到的消息顯示給用戶時,我遇到了問題。 我已經設法顯示在吐司消息,但我想使用警報對話框。 這是我GCMBaseIntentService:GCM-在GCMBaseIntentService中顯示AlertDialog
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();
}
*/
}
});
}
}
註釋的部分是我的嘗試顯示一個警告Dialog.I認爲上下文是問題,但我真的不知道如何解決它。 如果我爲此使用通知會更好?