在后台解析推送通知

时间:2015-12-18 10:35:10

标签: android json parse-platform push-notification

我的问题是,当我在我的应用程序中时,我能够解析推送通知,但是当应用程序在后台时,我在Parse包中收到了我的JSON,但没有进入我的活动!即当我点击通知栏时,我什么也得不到。

这是解析代码:

@Override
protected void onPushReceive(Context context, Intent intent) {
    super.onPushReceive(context, intent);

    if (intent == null)
        return;

    try {
        JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

        parseIntent = intent;

        parsePushJson(context, json);

    } catch (JSONException e) {
        Log.d("PushJsonException", ""+e.getMessage());
    }
}

private void parsePushJson(Context context, JSONObject getJson) {
    Log.d("ParsePushJson", getJson.toString());

    String title = "Twine";
    String message = getJson.toString();

    try {
        boolean isBackground = false;

        if (!isBackground) {
            Intent resultIntent = new Intent(context, Dashboard.class);
            showNotificationMessage(context, title, message, resultIntent);
        }

    } catch (Exception e) {
        e.printStackTrace();
        Log.d("JsonExe", e.toString());
    }
}

private void showNotificationMessage(Context context, String title, String message,
                                     Intent resultIntent) {
    notificationUtils = new NotificationUtils(context);

    resultIntent.putExtras(parseIntent.getExtras());

    resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    notificationUtils.showNotificationMessage(title, message, resultIntent);

}

@Override
protected void onPushDismiss(Context context, Intent intent) {
    super.onPushDismiss(context, intent);
}

@Override
protected void onPushOpen(Context context, Intent intent) {
    super.onPushOpen(context, intent);
}

这是解析它的类:

  @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    int i = 0;
    parseMessage = intent.getStringExtra("message");
    Log.d("ParseTwineMsg", parseMessage);
    AppController.getInstance().deletedList.add(parseMessage);


    if (AppController.getInstance().deletedList.size() > 0)
    {

        Log.d("ParseMsgSize", ""+AppController.getInstance().deletedList.size());

        notificationCounterLt.setVisibility(View.VISIBLE);
        notificationCounterTxt.setText("" + AppController.getInstance().deletedList.size());
    }
    feedNotificationArrayList.clear();

    try
    {

        for (i = 0; i < AppController.getInstance().deletedList.size(); i++)
        {

            String setUp = "setupPayments";

            JSONObject notificationJsonObj = new JSONObject(AppController.getInstance().deletedList.get(i));
            String alert = notificationJsonObj.getString("alert");

            JSONObject payloadJsonObj = notificationJsonObj.optJSONObject("payload");
            String notificationType = payloadJsonObj.optString("notificationType");
            int AmountToReceive = payloadJsonObj.optInt("pendingAmountToReceiveAsCents");

            JSONObject transactionObj = payloadJsonObj.optJSONObject("transaction");


            if( transactionObj != null)
            {

                JSONObject receiverObj = transactionObj.optJSONObject("receiver");
                JSONObject senderObj = transactionObj.optJSONObject("sender");
                transactionId = transactionObj.optInt("id");
                transactionType = transactionObj.optString("transactionType");
                creatorId = transactionObj.optInt("creatorId");
                status = transactionObj.optString("status");
                receiverId = receiverObj.optInt("id");
                senderId = senderObj.optInt("id");
                comment = transactionObj.optString("comment");
                amountAsCents = transactionObj.optInt("amountAsCents");
                dateCreated = transactionObj.optString("dateCreated");


                String senderName = senderObj.optString("firstName");
                String senderPic = senderObj.optString("pictureUrl");
                String receiverPic = receiverObj.optString("pictureUrl");
                String receiverName = receiverObj.optString("firstName");

                if (transactionObj.has("rejectionComment")) {
                    rejectionComment = transactionObj.optString("rejectionComment");
                }


                Editor editor = prefs.edit();
                editor.putInt("transactionId" + transactionId, transactionId);
                editor.commit();

                feedNotification = new FeedNotification();


                feedNotification.setNotificationType(notificationType);
                feedNotification.setTransactionId(transactionId);
                feedNotification.setTransactionType(transactionType);
                feedNotification.setNotificationAlertMsg(alert);
                feedNotification.setRejectionComment(rejectionComment);
                feedNotification.setAmountAsCents(amountAsCents);
                feedNotification.setComment(comment);
                feedNotification.setDateCreated(dateCreated);
                feedNotification.setSenderName(senderName);
                feedNotification.setReceiverPics(receiverPic);
                feedNotification.setReceiverName(receiverName);
                feedNotification.setSenderPics(senderPic);
                feedNotificationArrayList.add(feedNotification);

                Gson gson = new Gson();

                String contactCacheList = prefs.getString("friendsList", null);

                Type contactType = new TypeToken<ArrayList<Friends>>()
                {
                }.getType();

                editor = prefs.edit();
                gson = new Gson();

                String newNotificationList = gson.toJson(feedNotificationArrayList);
                String dataSize = gson.toJson(AppController.getInstance().deletedList);
                editor.putString("notificationList", newNotificationList);
                editor.putString("dataSize",dataSize);
                editor.apply();
            }
            Log.d("TwineNotificationSize", i + " --> " + feedNotificationArrayList.size() + " => " + AppController.getInstance().deletedList.get(i));

        }
        Log.d("TwineNotificationSize", "Final Size => " + feedNotificationArrayList.size());
    }
    catch (JSONException e)
    {
        e.printStackTrace();
        Log.d("NotificationException", e.toString());
    }
}

清单文件:

   <receiver android:name="com.parse.ParseBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
        </intent-filter>
    </receiver>
    <receiver
        android:name=".Parse.ParsePushReceive"
        android:exported="false">
        <intent-filter>
            <action android:name="com.parse.push.intent.RECEIVE" />
            <action android:name="com.parse.push.intent.DELETE" />
            <action android:name="com.parse.push.intent.OPEN" />
        </intent-filter>
    </receiver>

2 个答案:

答案 0 :(得分:1)

所以我通过覆盖:

来解决这个问题
@Override
protected void onPushOpen(Context context, Intent intent) {
super.onPushOpen(context, intent);

// On Tap of Notification
// Do Stuff Here
}

答案 1 :(得分:0)

我认为你没有将Content Intent设置为你的通知。试试这样:

PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        mContext,
                        0,
                        intent,
                        PendingIntent.FLAG_CANCEL_CURRENT
                );    

 Notification notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
                .setAutoCancel(true)
                .setContentTitle(title)
                .setStyle(inboxStyle)
                .setContentIntent(resultPendingIntent)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
                .setContentText(message)
                .build();
相关问题