1
我從我的服務器發送GCM消息,我的應用程序。解析收到的GCM消息到推送通知
通知工作樣本數據,但是當我試圖從我的服務器使用接收到的報文信息,我得到空值。
的是一個信息,我從我的服務器上獲取exmplae:(在showNotification收到MSG())
Received: {
"subtitle": "text",
"sound": "1",
"message": "bla bla",
etc..
這是我如何試圖處理它(查找showNotification() ):
public class GcmService extends GcmListenerService {
String title;
@Override
public void onMessageReceived(String from, Bundle data) {
JSONObject jsonObject = new JSONObject();
Set<String> keys = data.keySet();
for (String key : keys) {
try {
jsonObject.put(key, data.get(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
sendNotification("Received: " + jsonObject.toString(5));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onDeletedMessages() {
sendNotification("Deleted messages on server");
}
@Override
public void onMessageSent(String msgId) {
sendNotification("Upstream message sent. Id=" + msgId);
}
@Override
public void onSendError(String msgId, String error) {
sendNotification("Upstream message send error. Id=" + msgId + ", error" + error);
}
private void sendNotification(final String msg) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
MainActivity.mTextView.setText(msg);
//JSON Parsing
try {
JSONObject thePush = new JSONObject(msg);
JSONArray pushData;
pushData = thePush.optJSONArray("Received");
thePush = pushData.optJSONObject(0);
if (thePush != null) {
//Initalize data from my JSON
title = thePush.optString("title");
}
} catch (JSONException e) {
e.printStackTrace();
}
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.beer)
.setContentTitle(title)
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(1, mBuilder.build());
}
});
}
}
當我從下面的代碼收到GCM消息時,我得到一個沒有標題的消息。 由於值不是來自我的json,因此測試,身體工作。
什麼是與我收到的JSON方式的問題?
可以添加'你得到的'sendNotification'方法msg'您題 ? –
這是:'收到:{ 「字幕」:「文字」, 「聲音」:「1」, 「消息」:「bla bla」, etc ..(我發佈的消息) – EliKo
May是你是不是一個有效的JSON,在這裏發表您的JSON在http://jsonlint.com/,並檢查它是否有效與否 –