当应用程序处于后台时,不会出现消息

时间:2019-11-13 07:30:51

标签: android firebase-cloud-messaging

我在我的应用程序中使用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);
        }
    }

3 个答案:

答案 0 :(得分:0)

在棉花糖下面,您必须使用 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());

答案 1 :(得分:0)

在以下情况下,使Firebase库调用您的 onMessageReceived()

前台应用

后台应用

应用已被杀死

您不得在对Firebase API的请求中放入JSON密钥“ notification”,而应使用“ data”。

答案 2 :(得分:0)

如果您的应用程序在后台运行,并且同时发送通知消息和数据消息,则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
    }
}