【问题标题】:Android Firebase CloudMessaging doesn't load URL when App is closed it loads default page when clickedAndroid Firebase CloudMessaging 在应用程序关闭时不加载 URL 它在单击时加载默认页面
【发布时间】:2020-01-23 07:23:20
【问题描述】:

我正在使用 FCM 并向 Android 发送推送消息,当应用程序打开并收到推送消息时,URL 意图现在可以通过 TAG 字符串发送。当APP关闭并收到推送消息时,它会加载默认页面。

使用通知的要点:区域是我可以使用channel_id。如果我在 data: JSON 中发送此信息,它不再监听 channel_id 但推送消息都会打开正确的 URL,无论应用程序是打开还是关闭。

希望我们在这方面有一些专业人士使用 FCM 并能看到我的陷阱。谢谢!

我的 Firebase 消息服务

    public class MyMessagingService extends FirebaseMessagingService {

        protected String title;
        protected String body;
        protected String tag;

    @Override
    public void onNewToken(String token) {
        Log.d(TAG, "Refreshed token: " + token);
    }

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

        if (remoteMessage.getData().isEmpty()) {
            showNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody(),remoteMessage.getNotification().getTag());
        } else
            showNotification(remoteMessage.getData());

    }

    private void showNotification(Map<String, String> data) {

        String title = data.get("title");
        String body = data.get("body");
        String tag = data.get("tag");

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        if (tag != null) {
            intent.putExtra("url", tag);
        }

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID_1 = "1";
        String NOTIFICATION_CHANNEL_ID_2 = "2";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            NotificationChannel notificationChannel1 = new NotificationChannel(NOTIFICATION_CHANNEL_ID_1,"Critical Alerts",NotificationManager.IMPORTANCE_HIGH);
            notificationChannel1.setDescription("Critical Warnings from your Tubs");
            notificationChannel1.enableLights(true);
            notificationChannel1.setVibrationPattern(new long[]{0,1000,500,1000});
            notificationManager.createNotificationChannel(notificationChannel1);

            NotificationChannel notificationChannel2 = new NotificationChannel(NOTIFICATION_CHANNEL_ID_2,"Status Updates",NotificationManager.IMPORTANCE_LOW);
            notificationChannel2.setDescription("Status and Changes");
            notificationChannel2.enableLights(true);
            notificationChannel2.setVibrationPattern(new long[]{0,1000,500,1000});
            notificationManager.createNotificationChannel(notificationChannel2);
        }
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID_1);
            notificationBuilder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(body);
            notificationBuilder.setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(body));
            notificationBuilder.setContentIntent(pendingIntent);
            notificationManager.notify(new Random().nextInt(),notificationBuilder.build());
    }

    //App Closed
    public void showNotification(String title2, String body2, String tag2) {

        this.title = title2;
        this.body = body2;
        this.tag = tag2;

        Intent intent2 = new Intent(this, MainActivity.class);
        intent2.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        if (tag2 != null) {
            intent2.putExtra("url", tag2);
        }

        intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationManager notificationManager2 = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID_1 = "1";
        String NOTIFICATION_CHANNEL_ID_2 = "2";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            NotificationChannel notificationChannel3 = new NotificationChannel(NOTIFICATION_CHANNEL_ID_1,"Critical Alerts",NotificationManager.IMPORTANCE_HIGH);
            notificationChannel3.setDescription("Critical Warnings from your Tubs");
            notificationChannel3.enableLights(true);
            notificationChannel3.setVibrationPattern(new long[]{0,1000,500,1000});
            notificationManager2.createNotificationChannel(notificationChannel3);

            NotificationChannel notificationChannel4 = new NotificationChannel(NOTIFICATION_CHANNEL_ID_2,"Status Updates",NotificationManager.IMPORTANCE_LOW);
            notificationChannel4.setDescription("Status and Changes");
            notificationChannel4.enableLights(true);
            notificationChannel4.setVibrationPattern(new long[]{0,1000,500,1000});
            notificationManager2.createNotificationChannel(notificationChannel4);
        }
        NotificationCompat.Builder notificationBuilder2 = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID_1);
        notificationBuilder2.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title2)
                .setContentText(body2);
        notificationBuilder2.setStyle(new NotificationCompat.BigTextStyle()
                .bigText(body2));
        notificationBuilder2.setContentIntent(pendingIntent2);
        notificationManager2.notify(new Random().nextInt(),notificationBuilder2.build());
    }
}

我的 JSON 发送到 FCM HTTP v1

{
  "message": {
    "token": "dWdgiBqeZCM:APA91bEcufQdXdntHM8uN7PiqTEN2n_CxBF-lbqtC3yFJyF1p8c5qUzf5AfAa33XtQ30bOZxUD1423ia9i071rYvKh8MMGhEhG_DMeQ7EYTIryHoFILlGoTPOuLjdLy-8hqbYdE-MvgF",
    "android": {
      "notification": {
        "title": "Test Title!",
        "body": "Test Body!",
        "channel_id": "1",
        "tag": "https://example.com/"
      }
    }
  }
}

【问题讨论】:

  • 感谢参考我确实让它工作了,所以我需要更清楚地知道我在使用数据标签时它工作了所以“使用通知的要点:区域是这样我可以使用 channel_id”,因为它在放置在数据标签中时不使用 channel_id,因此它使用我的默认高优先级通道。我希望在数据字段中发送一个 URL 推送,并让它在不使用通知标签的情况下点击我的 channel_id。谢谢大家!

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


【解决方案1】:

你需要改变json;

 {
   "message": {
"token": "dWdgiBqeZCM:APA91bEcufQdXdntHM8uN7PiqTEN2n_CxBF-lbqtC3yFJyF1p8c5qUzf5AfAa33XtQ30bOZxUD1423ia9i071rYvKh8MMGhEhG_DMeQ7EYTIryHoFILlGoTPOuLjdLy-8hqbYdE-MvgF",
"android": {
  "notification": {     //Here you need to change here, 'notification' to 'data'
    "title": "Test Title!",      
    "body": "Test Body!",       
    "channel_id": "1",            
    "tag": "https://example.com/" 
    }
    }
  }
}

【讨论】:

  • 感谢@ashok,它正在工作,但我的主要问题应该放在 DOH 主题中!是如何在不使用通知标签的情况下传递 channel_id。它确实可以打开或关闭并正确传递 URL,但除非使用通知,否则不会收听我的 channel_id,如果我使用通知,它不会收听我的 URL。基本上使用数据标签查找上面的所有内容,但让它听我的 channel_id 而不是忽略它
【解决方案2】:

FCM Json 包含通知标签,这就是为什么onMessageReceived 在应用程序关闭时不调用。所以去掉这个通知标签

 "notification": {
    "title": "Test Title!",
    "body": "Test Body!",
    "channel_id": "1",
    "tag": "https://example.com/"
  }

像这样使用data标签而不是notification

 "data": {
    "title": "Test Title!",
    "body": "Test Body!",
    "channel_id": "1",
    "tag": "https://example.com/"
   }

并通过remoteMessage.getData()获取数据字段

【讨论】:

  • 谢谢@frankenstein。你是对的,因为当我在测试之前它工作得很好,如果应用程序打开或关闭,只要我只使用 data: tag 但是当我在 data: tag 中指定 channel_id 时,它被忽略并且 FCM 显示 channel_id 仅支持通知:JSON 的区域。我想在正文中发送所有内容,但是第二次使用该标签时,它的行为有所不同并使用默认通道。也许我需要在我的服务中添加一些东西来在数据标签中找到 channel_id 但不知道那会是什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-27
  • 2020-10-18
相关资源
最近更新 更多