Firebase推送通知?

时间:2018-09-24 09:58:09

标签: android firebase push-notification firebase-cloud-messaging

我正在我的应用程序中实现firebase推送通知,该应用程序还显示了推送通知。但是我的问题是-我可能会根据不同的主题发送不同类型的推送通知。

例如- 1.如果我要在App Update上发送推送通知,则在用户单击该通知时,应将他重定向到PlayStore

  1. 如果我要发送有关“新内容”的推送通知,则在用户单击该通知时,应将他重定向到App的MainActivity

  2. 如果我要在“新竞赛”中发送推送通知,则在用户单击该通知时,应将他重定向到App的ContestActivity

我在“ MessageReceiver”类中使用以下代码将其实现为-

public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        String title = remoteMessage.getNotification().getTitle();
        String message = remoteMessage.getNotification().getBody();

        showNotifications(title, message);
    }
private void showNotifications(String title, String msg) {
        Intent i = null;

        if(title.equals("DoUp Now - Update")){
            Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=com.s2s.doupnow"); // missing 'http://' will cause crashed
            i = new Intent(Intent.ACTION_VIEW, uri);
        }
        else if(title.equals("DoUp Now - New Content")){
            i = new Intent(this, MainActivity.class);
        }
        else if(title.equals("DoUp Now - New Notification")){
            i = new Intent(this, NotificationActivity.class);
        }

        PendingIntent pendingIntent = PendingIntent.getActivity(this, REQUEST_CODE,
                i, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationManager mNotifyManager;
        NotificationCompat.Builder mBuilder, mBuilderOld;

        final int NOTIFICATION_ID = 1;
        final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";

        mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);

            // Configure the notification channel.
            notificationChannel.setDescription("Channel description");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableVibration(true);
            mNotifyManager.createNotificationChannel(notificationChannel);

            mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
            mBuilder.setContentTitle(title)
                    .setContentText(msg)
                    .setSmallIcon(R.drawable.doupnowlogo)
                    .setOnlyAlertOnce(true)
                    .setContentIntent(pendingIntent);

            mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
        }
        else
        {
            mBuilderOld = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
            mBuilderOld.setContentTitle(title)
                    .setContentText(msg)
                    .setSmallIcon(R.drawable.doupnowlogo)
                    .setOnlyAlertOnce(true)
                    .setContentIntent(pendingIntent)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setPriority(NotificationManager.IMPORTANCE_MAX);

            mNotifyManager.notify(NOTIFICATION_ID, mBuilderOld.build());
        }
    }

但是在用户单击任何通知时,他总是始终仅重定向到App的MainActivity。

有人可以引导我我想念的地方吗?

1 个答案:

答案 0 :(得分:0)

  1. 如果要使用api比较标题,则必须使用 JSON 对象检查标题,就像这样。

    JSONObject data = json.getJSONObject(“ data”);

        String title = data.getString("title");
        String message = data.getString("message");
    
  2. 确保获得的标题与要比较的标题相同,并在意图中添加标志。

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);