Android 通知 PendingIntent 中的 Intent 被覆盖的问题

在Android开发中,创建通知时遇到PendingIntent的Intent被覆盖问题。当多个通知同时显示,最后一个通知的Intent会覆盖之前的Intent。通过在PendingIntent.getActivity()中修改requestCode参数,成功解决了此问题,但其工作原理尚待深入理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近开发中使用到了通知栏,产品需要统计通知栏的的展示和点击。展示比较方便,直接在通知栏 notify() 时进行上传统计即可;点击使用到了Intent 传递参数,然后在 MainActivity 的 onNewIntent() 中读取 Intent,再上传统计即可。

public PendingIntent getPendingIntent() {
        Intent intent = new Intent(App.getInstance(), MainActivity.class);
        int entrance = PushManger.getInstance(App.getInstance()).getEntrance();
        intent.putExtra(PushManger.ENTRANCE, entrance);
        return PendingIntent.getActivity(App.getInstance(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

由于点击通知栏打开的是同一个 Activity,仅通知 id 不一样,发现同时弹出多个通知时,最后一个通知的 Intent 会覆盖之前通知的 Intent。content 参数是固定的,intent 参数的 Extra 已经不同了,修改 PendingIntent.getActivity() 的 Flags 参数也没用,剩下唯一相同的参数就是 requestCode 参数了,最后尝试修改 requestCode 参数,竟然神奇的解决了问题。但关于这个参数,Google 并没有给出更多的解释,这里权且记录下问题的解决,原理还有待挖掘。

    /**
     * Retrieve a PendingIntent that will start a new activity, like calling
     * {@link Context#startActivity(Intent) Context.startActivity(Intent)}.
     * Note that the activity will be started outside of the context of an
     * existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK
     * Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent.
     *
     * <p class="note">For security reasons, the {@link android.content.Intent}
     * you supply here should almost always be an <em>explicit intent</em>,
     * that is specify an explicit component to be delivered to through
     * {@link Intent#setClass(android.content.Context, Class) Intent.setClass}</p>
     *
     * @param context The Context in which this PendingIntent should start
     * the activity.
     * @param requestCode Private request code for the sender
     * @param intent Intent of the activity to be launched.
     * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE},
     * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT},
     * or any of the flags as supported by
     * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
     * of the intent that can be supplied when the actual send happens.
     *
     * @return Returns an existing or new PendingIntent matching the given
     * parameters.  May return null only if {@link #FLAG_NO_CREATE} has been
     * supplied.
     */
    public static PendingIntent getActivity(Context context, int requestCode,
            Intent intent, @Flags int flags) {
        return getActivity(context, requestCode, intent, flags, null);
    }
public PendingIntent getPendingIntent() {
        Intent intent = new Intent(App.getInstance(), MainActivity.class);
        int entrance = PushManger.getInstance(App.getInstance()).getEntrance();
        intent.putExtra(PushManger.ENTRANCE, entrance);
        return PendingIntent.getActivity(App.getInstance(), entrance, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值