点击通知时不打开 Play Market

Not open Play Market when tap on notification

我想在点击通知但打开应用程序时打开 Play Market。 我使用 firebase 通知。

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        showNotification(remoteMessage.getNotification().getBody());
    }

    private void showNotification(String message) {
        String textFirebaseEn = getResources().getString(R.string.notification_text_firebase_checking);
        if (message.equals(textFirebaseEn)) {
            String localTitle = getResources().getString(R.string.notification_title_firebase);
            String localText = getResources().getString(R.string.notification_text_firebase);
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
                    .setSmallIcon(R.drawable.avatar_notification)
                    .setContentTitle(localTitle)
                    .setContentText(localText)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(PLAY_MARKET));

            builder.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, intent, 0));
            Notification notification = builder.build();
            NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
            manager.notify(0, notification);
        }
    }

请告诉我哪里错了?

您的代码仅适用于前台通知。如果您在应用内并收到通知,当您点击它时,Play Store 应该会打开。如果没有,试试这个:

final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
try {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}

答案来自:How to open the Google Play Store directly from my Android application?

如果您还想通过后台通知点击打开 Play 商店,最好的方法是创建一个新的 activity:

<activity android:name="OpenPlayStoreActivity"
        android:screenOrientation="portrait">
    </activity>

在来自 firebase 函数的负载中,您应该放置 "click_action": "OpenPlayStoreActivity"

并且在您的 OpenPlayStoreActivity 中:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
        try {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
        } catch (android.content.ActivityNotFoundException anfe) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
        }
}