0

我正在开发通过 GCM 获取 Json 消息并通过 Bundles 传递通知的 Android 应用程序。一切正常,我在通知托盘中收到 Bundles 消息。

但我想解析那个 JSON Bundle 消息,它给了我一个错误:

java.lang.String 类型的 Bundle 无法转换为 JSONArray

下面给出了我通过 Bundles 发出的 JSON 响应以及代码。

Bundle[{summary=Game update 49ers touchdown, android.support.content.wakelockid=1, collapse_key=do_not_collapse, from=931953845039, lastplay=5yd run up the middle}]

服务等级:

public class GcmIntentService extends IntentService {
    public static final int NOTIFICATION_ID = 1;
    private static final String TAG = "PubnubGcm";
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;

    public GcmIntentService() {
        super("GcmIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {

            if (GoogleCloudMessaging.
                    MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification("Deleted messages on server: " +
                        extras.toString());
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());

                sendNotification(extras.toString());


                try {
                    JSONArray jsonArray = new JSONArray(extras.toString());


                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject c = jsonArray.getJSONObject(i);
                        System.out.println(c.getString("summary"));

                        /*System.out.println(c.getString(APP_URL));
                          System.out.println(c.getString(APP_NAME));
                          System.out.println(c.getString(APP_IMAGE));
                          System.out.println(c.getString(IMAGE_TYPE));
                          System.out.println(c.getString(APP_TRACK));*/
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


                Log.i(TAG, "Received: " + extras.toString());
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }


    private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.icon)
        .setContentTitle("GCM Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}
4

0 回答 0