How to open Play Store via a FCM notification?

2019-08-07 12:20发布

I would like to send a notification to my users to remember them to update the app, but I can't change the app code as obviously the problem is that the app is outdated.

Is there a way to open Play Store via a FCM notification, using clickAction or something like that, without needing to make changes to the app?

2条回答
神经病院院长
2楼-- · 2019-08-07 12:54

you can open play store-specific app using FCM you just need to put following logic in your onMessageReceived of your FCM custom class.

Bellow is the Logic:

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());
        }
    }
查看更多
男人必须洒脱
3楼-- · 2019-08-07 13:09

No without changing in code you can't make that. You have to overwrite onMessageReceived as in here ( How to handle notification when app in background in Firebase ) and then set the URL of it as in here ( Opening a browser link through notification isn't working ). Reference https://stackoverflow.com/a/42339262/9252060

查看更多
登录 后发表回答