簡體   English   中英

將收到的GCM消息解析為推送通知

[英]Parsing received GCM message into push notification

我正在從服務器向應用程序發送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的方式有什么問題?

您正在接收的數據已經是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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM