【问题标题】:Message doesn't appear when app is in background应用程序在后台时不显示消息
【发布时间】:2019-11-13 07:30:51
【问题描述】:

我在我的应用程序中使用 FCM,在 onmessagerecieve() 函数中我执行一些功能。如果远程消息是通知或数据消息,我会实现这两种方法。 我的应用中有四种情况

  • 当应用程序在前台时接收通知消息(它可以正常工作)
  • 当应用程序在前台时接收数据消息(它可以正常工作)
  • 当应用程序在后台时接收通知消息(它可以正常工作)
  • 当应用处于后台时接收数据消息(这给我带来了问题

问题是当我收到数据消息并且应用程序处于后台时......没有任何通知或执行原始功能都没有发生。 当应用程序处于前台时,我想在收到数据消息时执行相同的功能

  public void onMessageReceived(RemoteMessage remoteMessage) {
        Map data = remoteMessage.getData();
        RemoteMessage.Notification notification = remoteMessage.getNotification();

        if (data.isEmpty()) {
            System.out.println("FCM type is Notification");
            parseNotificationMessage(remoteMessage);

        } else {
            System.out.println("FCM type is Data");
            parseDataMessage(remoteMessage);
        }

    }

    private void parseNotificationMessage(RemoteMessage remoteMessage) {
        String notification_title =remoteMessage.getNotification().getTitle();

        String notification_message = remoteMessage.getNotification().getBody();

        System.out.println("1234567::   "+notification_title);
        System.out.println("1234567::   "+notification_message);
        String first_letter = "";
        String title = "";
        try {
            first_letter = notification_title.substring(0, 1);
            title = notification_title.substring(1);
        }
        catch (Exception e){
        }

        if (first_letter.equals("@")&& title.equals("NewOrder")) {
            System.out.println("1234567::   "+notification_title);
            System.out.println("1234567::   "+notification_message);

            Intent intent = new Intent("JSON_DATA");
            Bundle bundle = new Bundle();
            bundle.putString("Notification_JSON",notification_message );
            bundle.putString("Signal","NewOrder");
            intent.putExtras(bundle);
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);


        }
        else if (first_letter.equals("@")&& title.equals("CancelOrder")) {
            System.out.println("1234567::   "+notification_title);
            System.out.println("1234567::   "+notification_message);

            Intent intent = new Intent("JSON_DATA");
            Bundle bundle = new Bundle();
            bundle.putString("Notification_JSON",notification_message );
            bundle.putString("Signal","CancelOrder");
            intent.putExtras(bundle);
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

        }

        else {

            generateNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
            System.out.println("1234567::   "+notification_title);
            System.out.println("1234567::   "+notification_message);
        }
    }

    private void parseDataMessage(RemoteMessage remoteMessage) {
        String notification_title =remoteMessage.getData().get("title");

        String notification_message = remoteMessage.getData().get("message");

        System.out.println("1234567::   "+notification_title);
        System.out.println("1234567::   "+notification_message);
        String first_letter = "";
        String title = "";
        try {
            first_letter = notification_title.substring(0, 1);
            title = notification_title.substring(1);
        }
        catch (Exception e){
        }

        if (first_letter.equals("@")&& title.equals("NewOrder")) {
            System.out.println("1234567::   "+notification_title);
            System.out.println("1234567::   "+notification_message);

            Intent intent = new Intent("JSON_DATA");
            Bundle bundle = new Bundle();
            bundle.putString("Notification_JSON",notification_message );
            bundle.putString("Signal","NewOrder");
            intent.putExtras(bundle);
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);


        }
        else if (first_letter.equals("@")&& title.equals("CancelOrder")) {
            System.out.println("1234567::   "+notification_title);
            System.out.println("1234567::   "+notification_message);

            Intent intent = new Intent("JSON_DATA");
            Bundle bundle = new Bundle();
            bundle.putString("Notification_JSON",notification_message );
            bundle.putString("Signal","CancelOrder");
            intent.putExtras(bundle);
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

        }

        else {

            generateNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("message"));
            System.out.println("1234567::   "+notification_title);
            System.out.println("1234567::   "+notification_message);
        }
    }

【问题讨论】:

  • 您应该在这里查看我的旧答案:stackoverflow.com/questions/58586867/…
  • @JohnLe 实际上是在消息类型为数据且应用程序处于后台时运行 onmessagerecieve() .. 可能吗?
  • 在哪个版本的android设备中产生问题(低于orio或来自orio和高于orio)
  • 低于奥利奥目前我正在棉花糖上测试
  • DataMessage push 将被接收应用程序后台/前台而不是NotificationMessage push

标签: android firebase-cloud-messaging


【解决方案1】:

在 marshmallow 下方,您必须使用 NotificationCompat.Builder 向您显示消息,并且您必须使用 NotificationChannal > 大于或等于棉花糖

     NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(mContext.getApplicationContext(), "notify_001");
    Intent ii = new Intent(mContext.getApplicationContext(), RootActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, ii, 0);

    NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
    bigText.bigText(verseurl);
    bigText.setBigContentTitle("Title");
    bigText.setSummaryText("Text in detail");

    mBuilder.setContentIntent(pendingIntent);
    mBuilder.setSmallIcon(R.mipmap.ic_launcher_round);
    mBuilder.setContentTitle("Your Title");
    mBuilder.setContentText("Your text");
    mBuilder.setPriority(Notification.PRIORITY_MAX);
    mBuilder.setStyle(bigText);

    NotificationManager mNotificationManager =
            (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("notify_001",
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        mNotificationManager.createNotificationChannel(channel);
    }

    mNotificationManager.notify(0, mBuilder.build());

【讨论】:

    【解决方案2】:

    让firebase库在以下情况下调用你的onMessageReceived()

    前台应用

    后台应用

    应用已被杀死

    您不得在对 firebase API 的请求中添加 JSON 密钥“通知”,而应使用“数据”。

    【讨论】:

      【解决方案3】:

      当您的应用程序处于后台时,如果您同时发送通知和数据消息,您将不会在 onMessageReceived() 内收到任何通知消息或数据消息。通知消息将作为通知传递到系统托盘,数据消息将发送到单击通知时启动的活动意图的附加部分。您可以做的是仅发送带有通知标题和描述的数据消息,以便您可以将它们显示为通知或做任何您想做的事情。另一方面,当从额外的意图中单击通知时,您可以处理您的数据消息,如下所示:

      从“.MainActivity”的onCreate中的通知中获取“数据”信息:

      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          //get notification data info
          Bundle bundle = getIntent().getExtras();
          if (bundle != null) {
             //Handle data
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-24
        • 2020-04-05
        • 2023-02-01
        • 2015-11-25
        • 2021-03-12
        • 1970-01-01
        • 2012-12-25
        • 1970-01-01
        相关资源
        最近更新 更多