【问题标题】:Parsing received GCM message into push notification将收到的 GCM 消息解析为推送通知
【发布时间】:2016-01-01 23:43:23
【问题描述】:

我正在将 GCM 消息从我的服务器发送到我的应用程序。

通知适用于示例数据,但当我尝试使用从服务器接收到的消息信息时,我得到空值。

我从我的服务器收到的一条消息的示例:(在 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吗?
  • 这是它:Received: { "subtitle": "text", "sound": "1", "message": "bla bla", etc..(我发布的消息)
  • 可能你的 json 不是有效的 json,请将你的 json 发布到 jsonlint.com 并检查它是否有效
  • 我认为你是对的,它返回错误,那么我如何处理这个接收到的数组?
  • 您需要将其从服务器转换为有效的 json 格式,然后发送到应用程序

标签: android


【解决方案1】:

您收到的数据已经是json 格式,因此您可以执行以下操作从相应的key 获取value

@Override
public void onMessageReceived(String from, Bundle data) {
    String subtitle = data.getString("subtitle","defValue");
    String sound = data.getString("sound","defValue");
    String message = data.getString("message","defValue");
    //..... fetch other values similarly 

    Log.d("Data ->",subtitle+"-"+sound+"-"+message);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多