【问题标题】:Not open Play Market when tap on notification点击通知时未打开 Play Market
【发布时间】:2017-09-18 12:59:13
【问题描述】:

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

请告诉我哪里错了?

【问题讨论】:

  • 不要使用直接市场 URI,而是创建一个空白 Activity 并将 contentIntent 重定向到该空白 Activity 并从该空白 Activity 处理您的 Play 商店重定向。

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


【解决方案1】:

您的代码仅适用于前台通知。如果您在应用程序内并收到通知,当您点击它时,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)));
        }
}

【讨论】:

  • 请解释一下这个地方:在你的 firebase 函数的有效载荷中,你应该把“click_action”:“OpenPlayStoreActivity”。
  • @nicolasasinovich 您想仅从 firebase 仪表板发送通知还是在发生某些事情时发送通知?
  • 在这种情况下 - 仅来自 firebase 仪表板
  • 比忽略有效载荷的部分。 Payload 与 firebase 功能一起使用,以便您可以在服务器上发生某些事情时自动发送通知。该视频解释了有关它的所有内容,它可能会对您有所帮助:youtube.com/…
  • 谢谢,最后一个问题,现在我只有在打开应用程序之前点击通知时才能打开 Play Market,如果没有打开应用程序,我可以打开 Play Market 吗?
猜你喜欢
  • 1970-01-01
  • 2019-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
  • 1970-01-01
相关资源
最近更新 更多