You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android通过FCM通知PendingIntent跳转Play Store及安装状态检测问题

问题原因
  • 原有检测逻辑使用了PackageManager.GET_UNINSTALLED_PACKAGES标记,该标记会返回已卸载但保留了数据的应用包信息,会把残留的Play Store包信息误判为已安装。
  • Google Play服务和Play Store客户端是两个独立应用,isGooglePlayServicesAvailable返回true仅代表服务可用,无法证明客户端已安装。
  • 创建Intent本身不会抛出异常,只有执行startActivity时找不到对应处理应用才会触发ActivityNotFoundException。PendingIntent是用户点击通知时由系统执行startActivity,异常不会被应用代码捕获,因此出现点击无响应的情况。
正确的Play Store安装状态检测方法

直接通过PackageManager查询当前实际安装且可用的Play Store包,同时判断应用是否被用户禁用:

private static final String PLAY_STORE_PACKAGE = "com.android.vending";

private boolean isPlayStoreAvailable(Context context) {
    PackageManager packageManager = context.getPackageManager();
    try {
        // 不添加GET_UNINSTALLED_PACKAGES标记,仅查询当前已安装的应用
        packageManager.getPackageInfo(PLAY_STORE_PACKAGE, 0);
        ApplicationInfo appInfo = packageManager.getApplicationInfo(PLAY_STORE_PACKAGE, 0);
        // 额外验证应用处于启用状态,排除被用户禁用的场景
        return appInfo.enabled;
    } catch (PackageManager.NameNotFoundException e) {
        // 未找到对应包,说明未安装
        return false;
    }
}
适配后的跳转逻辑

普通直接跳转场景

public void openAppPlayStorePage(Context context, String appPackageName) {
    Intent intent;
    if (isPlayStoreAvailable(context)) {
        // 已安装Play Store,用market协议跳转原生客户端
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
        intent.setPackage(PLAY_STORE_PACKAGE);
    } else {
        // 未安装Play Store,跳转网页版,不指定包名让系统选择浏览器打开
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
    }
    // 额外判断是否有应用可以处理该Intent,避免崩溃
    if (intent.resolveActivity(context.getPackageManager()) != null) {
        context.startActivity(intent);
    }
}

通知PendingIntent封装场景

封装PendingIntent前先判断Play Store状态,避免后续点击通知无响应:

public PendingIntent buildPlayStorePendingIntent(Context context, String appPackageName, int notificationId) {
    Intent intent;
    if (isPlayStoreAvailable(context)) {
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
        intent.setPackage(PLAY_STORE_PACKAGE);
    } else {
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
    }
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    // Android 12及以上需要指定PendingIntent的可变性,这里用FLAG_IMMUTABLE即可
    return PendingIntent.getActivity(
        context,
        notificationId,
        intent,
        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
    );
}

内容的提问来源于stack exchange,提问作者lannyf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 21:48:03