从解析中收到的堆栈通知

stack notification received from parse

我想从解析中堆叠多个收到的通知,当收到的通知多于 1 个通知并在一个通知中显示给用户。我搜索了很多,但找不到正确的解决方案。这可能吗?

您需要的是自定义 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 class 中,您实现了所有需要的关于显示推送的逻辑

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