gpt4 book ai didi

android - 在后台解析推送通知

转载 作者:行者123 更新时间:2023-11-29 01:25:28 25 4
gpt4 key购买 nike

我的问题是,当我在我的应用程序中时,我能够解析推送通知,但是当应用程序在后台时,我在 Parse 包中收到我的 JSON,但没有进入我的 Activity !即,当我点击通知栏时,我没有收到任何信息。

这是解析代码:

@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());
}
}

list 文件:

   <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>

最佳答案

所以我通过重写解决了这个问题:

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

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

关于android - 在后台解析推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34353602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com