「GCM通知」からデータを取得する方法はありますか?これが、gcmで送信するjson文字列の一部です{"data":{"id":"123"}}
。アプリでidの値を取得する必要がありますが、方法がわかりません...どうもありがとうございました。
31420 次
3 に答える
26
新しいGCMライブラリを使用している場合は、IntentServiceを拡張するクラスを作成する必要があります。これは、GCMメッセージが受信されたときにGCMライブラリが通知する場所です。MyIntentService.javaサンプルをご覧ください。
@Override
public final void onHandleIntent(Intent intent) {
try {
String action = intent.getAction();
if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {
handleRegistration(intent);
} else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) {
handleMessage(intent);
}
} finally {
synchronized(LOCK) {
sWakeLock.release();
}
}
}
private void handleMessage(Intent intent) {
String id = intent.getExtra("id");
}
GCMライブラリを使用していない場合は、レシーバーのインテントでGCM応答が届きます。次に、インテントのgetExtras()。getString()を使用して、GCM通知からキーと値のペアを取得できます。例えば
// intent come in in your onReceive method of your BroadcastReceiver:
public onReceive(Context context, Intent intent) {
// check to see if it is a message
if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
String id = intent.getExtras().getString("id");
String other_key = intent.getExtras().getString("other_key");
// if your key/value is a JSON string, just extract it and parse it using JSONObject
String json_info = intent.getExtras().getString("json_info");
JSONObject jsonObj = new JSONObject(json_info);
}
}
于 2012-07-04T15:51:10.633 に答える
6
json表現として取得する最良の方法は、データをjsonオブジェクトとして追加することです。
{
"registration_ids" : [
"id1",
"id2"
],
"data" : {
"my_json_object": {
"text" :"This is my message",
"title":"Some title"
}
},
"collapse_key":"12345"
}
次に、オブジェクトを解析するには、次のようにします。
String json = getIntent().getExtras().getString("my_json_object");
JsonObject jObject = new JsonObject(json);
于 2013-10-31T09:21:46.100 に答える
0
<receiver android:name=".beforelogin.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="android.intent.category.TAB" />
</intent-filter>
</receiver>
<service android:name=".beforelogin.GcmIntentService" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
于 2016-05-12T10:57:40.033 に答える