我希望在收到通知超过1时通过解析堆叠多个收到的通知,并在一个通知中显示给用户。我搜索了这么多,但我找不到合适的解决方案。那可能吗?
答案 0 :(得分:2)
你需要的是一个自己的PushReceiver,所以根据Parse文档,你在清单中声明了接收器
<receiver
<!-- Put here path to a class that will handle pushes -->
android:name="com.domain.ReceiverClass"
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>
在这个PushReceiver类中,您实现了有关显示推送的所有必需逻辑
public class PushReceiver extends ParsePushBroadcastReceiver {
@Override
protected void onPushReceive(Context context, Intent intent) {
//Don't call super.onPushReceive!
JSONObject pushData = new JSONObject(intent.getStringExtra(KEY_PUSH_DATA));
//Here is how you obtain data
String alert = pushData.optString("alert", "Notification received.");
//Any manipulations with stacking go here
}
}
修改即可。所以一切都取决于您的要求。例如,您可以在接收器中累积通知,并安排AlarmManager在5分钟后唤醒。将当前通知数作为额外通过,然后检查新推送是否到达。如果是这样 - 等待下一个警报,如果没有 - 显示所有内容。
SharedPreferences prefs = context.getSharedPreferences(KEY_NOTIFICATIONS,
Context.MODE_PRIVATE
);
JSONArray stacked = new JSONArray(prefs.getString(KEY_STACKED, ""));
stacked.put(alert);
prefs.edit().putString(KEY_STACKED, stacked.toString()).apply();
Intent i = new Intent(context, ActualProcessor.class);
i.putExtra(EXTRA_COUNT, stacked.length());
PendingIntent receiver = PendingIntent.getBroadcast(context, CODE, i, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(5),
receiver
);