点击通知时不打开Play Market

时间:2017-09-18 12:59:14

标签: android firebase push-notification notifications firebase-notifications

我想在点击通知但打开应用时打开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);
        }
    }

请告诉我有什么不对?

1 个答案:

答案 0 :(得分:2)

您的代码仅适用于前台通知。如果您在应用内并收到通知,则在点按该应用时,应启用Play商店。如果没有,请尝试:

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