android - 如何通过 FCM 通知打开 Play 商店?

标签 android firebase firebase-cloud-messaging

我想向我的用户发送通知,以记住他们更新应用程序,但我无法更改应用程序代码,因为显然问题是应用程序已过时。

有没有办法通过 FCM 通知、使用 clickAction 或类似的方式打开 Play 商店,而不需要对应用进行更改?

最佳答案

您可以使用 FCM 打开 Play 商店特定的应用程序,只需将以下逻辑放入 FCM 自定义类的 onMessageReceived 中即可。

以下是逻辑:

public static void openStore(Context context,RemoteMessage remoteMessage) {
        Intent intent;
        if (remoteMessage.getData().containsKey("appPackageName")) {
            final String appPackageName = remoteMessage.getData().get("appPackageName"); // getPackageName()
            try {
                intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
            } catch (android.content.ActivityNotFoundException anfe) {
                intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
            }
            int notificaionId = 1;
            PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
            NotificationCompat.BigTextStyle bigTextNotiStyle = null;
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            int color = ContextCompat.getColor(this, R.color.profileFontColor);
            NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
                    .setSmallIcon(R.mipmap.ic_notification)
                    .setContentTitle("" + remoteMessage.getData().get("title"))
                    .setContentText("" + remoteMessage.getData().get("desc"))
                    .setStyle(bigTextNotiStyle)
                    .setAutoCancel(true)
                    .setColor(color)
                    .setContentIntent(pIntent)
                    .setLights(Color.RED, 3000, 3000);
            notificationManager.notify(notificaionId, mBuilder.build());
        }
    }

关于android - 如何通过 FCM 通知打开 Play 商店?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48455519/

相关文章:

java.lang.NoClassDefFoundError : Failed resolution of: Lcom/google/android/gms/common/util/zzq;

android - 我们可以在 Firebase Cloud Messaging 中创建多少主题?

java - Firebase 在类上找不到要序列化的属性

应用程序启动时模拟器中的 Android 初始错误

android - ListView 项目不会将宽度扩展到 fill_parent

android - Firebase - 如何使用将在 firebase 测试实验室上运行的命令行运行特定的 Android UI 测试类?

ios - 使用 Firebase 实时数据库发送聊天应用程序构建的推送通知

java - 如何控制 Android 虚拟机?

javascript - 未捕获的语法错误 : Identifier 'baseUrl' has already been declared

javascript - Firebase 的云函数 - 使用 Promise 循环