0

I want open Play Market when tap on notification but opened app. I use firebase notification.

@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);
        }
    }

Please tell me what wrong?

KENdi
  • 7,670
  • 2
  • 15
  • 27
nicolas asinovich
  • 2,613
  • 2
  • 24
  • 34
  • 1
    Instead of using direct market URI create a blank Activity and redirect the contentIntent to that blank Activity and handle your play store redirection from that blank activity. – Kunu Sep 18 '17 at 13:22

1 Answers1

2

Your code works only for foreground notification. If you are inside the app and receive a notification, when you tap it, Play Store should open. If it doesn't, try this:

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)));
}

Answer from : How to open the Google Play Store directly from my Android application?

If you also want to open play store from background notification click, the best way to do it is to create a new activity:

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

In your payload from the firebase function, you should put "click_action": "OpenPlayStoreActivity".

And in your 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)));
        }
}
Radu Serse
  • 116
  • 9
  • please, explain this place: In your payload from the firebase function, you should put "click_action": "OpenPlayStoreActivity". – nicolas asinovich Sep 18 '17 at 14:42
  • @nicolasasinovich do you want to send notifications only from the firebase dashboard or send a notification when something happens? – Radu Serse Sep 18 '17 at 14:56
  • in this case - only from firebase dashboard – nicolas asinovich Sep 18 '17 at 18:54
  • 1
    Than ignore the part with the payload. Payload is used with firebase functions, so that you can send notifications automatically when something happens on the server. This video explains everything about it and it might help you: https://www.youtube.com/watch?v=lUHIknp36VM&list=PLGCjwl1RrtcQ3o2jmZtwu2wXEA4OIIq53&index=24 – Radu Serse Sep 18 '17 at 19:14
  • thanks, and last question, now I can open Play Market when tap on notification only if before i opened app, can i open Play Market if not opened app? – nicolas asinovich Sep 18 '17 at 19:26
  • Sure! That video series explains everything. Please mark my answer as correct if it helped you. Thanks! – Radu Serse Sep 18 '17 at 19:32
  • Happy to do it! Good luck, @nicolasasinovich – Radu Serse Sep 19 '17 at 11:10