更新通知 - 重用已存在的意图数据

时间:2017-12-31 15:09:02

标签: android notifications android-notifications

每当安装新应用时,我都会显示如下通知:

public static void showNotification(Context context, String appPackageName) {
        Intent intent = new Intent(context, NewAppFoundActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("appPackageName", appPackageName);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        String appName = AppUtil.getAppName(appPackageName);
        if (appName == null) {
            appName = appPackageName;
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationChannelManager.CHANNEL_ID)
                .setSmallIcon(R.drawable.icon_not)
                .setContentIntent(pendingIntent)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon))
                .setContentTitle(context.getString(R.string.new_app_found_title))
                .setContentText(context.getString(R.string.new_app_found_notification_info))
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText(context.getString(R.string.new_app_found_notification_info_long, appName)))
                .setColor(Color.parseColor("#25baa2"))
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)

                .setAutoCancel(true);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(BaseDef.NOT_ID_NEW_APP, builder.build());
    }

我的问题是,我希望在显示通知时更改此选项以支持多个安装。所以我有以下问题:

如何从显示的通知中获取我知道ID的意图?伪代码如下所示:

// !!! PSEUDO CODE !!!
Intent intent = null;
if (isNotificationWithIdShown(BaseDef.NOT_ID_NEW_APP)) {
    intent  = getNotificationIntent(BaseDef.NOT_ID_NEW_APP);
    // reuse this intent and add new apps instead of creating a new intent with a single app
} else {
    intent = new Intent(context, NewAppFoundActivity.class);
    // add first app to intent
}

任何想法如何实现这一目标?我想避免将数据保存在数据库/偏好中,这将是多余的,但这将是我的替代解决方案......

0 个答案:

没有答案