3

我正在尝试实现推送通知客户端。我想在 onMessage(Context arg0, Intent arg1) 函数中显示祝酒词。

这是我的代码:

    public class GCMIntentService extends GCMBaseIntentService {
static Context c;
@SuppressWarnings("hiding")
private static final String TAG = "GCMIntentService";

public GCMIntentService() {
    super("717816998404");
    c = this.getBaseContext();
}

/**
 * Issues a notification to inform the user that server has sent a message.
 */

@Override
protected void onError(Context arg0, String arg1) {
    // TODO Auto-generated method stub

}

@Override
protected void onMessage(Context arg0, Intent arg1) {
    Log.d("GCM", "RECIEVED A MESSAGE");
    Log.d("GCM", "RECIEVED A MESSAGE");
    Log.d("GCM", "RECIEVED A MESSAGE");
    Log.d("GCM", "RECIEVED A MESSAGE");
    Log.d("GCM", "RECIEVED A MESSAGE");
    Log.d("GCM", "RECIEVED A MESSAGE");
    Log.d("GCM", "RECIEVED A MESSAGE");
    String str =arg1.getExtras().getString("PVAL");

    Log.i("PVAL",str);
    // I want to display a toast here

}

@Override
protected void onRegistered(Context arg0, String arg1) {
    // TODO Auto-generated method stub
    Log.i("TEST", arg1);
    Log.e("TEST", arg1);

}

@Override
protected void onUnregistered(Context arg0, String arg1) {
    // TODO Auto-generated method stub

}

}

我想知道如何显示收到的消息我试图显示一个 toast 来检查消息。

4

2 回答 2

12

尝试这个:

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();
    }         
        });
于 2012-11-12T12:51:50.597 回答
3

在 onMessage 方法中添加 toast 创建怎么样?像这样:

@Override
protected void onMessage(Context arg0, Intent arg1) {
   String str = arg1.getExtras().getString("PVAL");
   Toast.makeText(arg0, str, Toast.LENGTH_LONG).show();
}

如果这不起作用,请按照 GCM 库中捆绑的 Demo 示例进行操作。演示示例在主活动中使用 BroadcastReceiver,接收器在 GCM 传入消息上显示对话消息。您可以改为将对话框显示更改为 toast 显示。

于 2012-07-06T14:55:15.577 回答