Android 应用在后台时通知不会展开

Android notifications not expanded when app in background

我正在尝试使用 FCM 发送通知,它按预期工作,但我面临着两种我想处理的不同行为,因为我正在使用 bigtextstyle 和一个操作:

第一种情况:应用在前台, onReceiveMessage 按预期工作并正确显示通知

第二种情况:应用程序被杀死或在后台, 文档说消息不在 onReceiveMessage 事件中处理,通知的布局不是 bigtextstyle:

我试图找到一种方法来显示所需的布局,但没有任何效果,他们说数据有效载荷是在启动器意图的额外内容中传送的 Activity!

所以,我想要的是在两种情况下显示相同的通知布局,因为有时我使用通知触发呼叫或在浏览器中打开 link 而不是打开应用本身。

我从 Firebase 控制台发送消息,如下所示:

这里是通知函数:

    private void notifyMe(RemoteMessage rm){
    String title = "";
    String body = "";
    String phone = "";
    String link = "";
    long time = System.currentTimeMillis();
    Intent iCall;
    PendingIntent pintCall = null;
    Intent notificationIntent = null;
    NotificationManager notificationManager;
    notificationManager =  (NotificationManager)FirebaseMessagingService.this.getSystemService(Context.NOTIFICATION_SERVICE);

    if (rm.getNotification() != null) {
        title = rm.getNotification().getTitle();
        body = rm.getNotification().getBody();
    }

    if (rm.getData().size() > 0) {
        phone = rm.getData().get("phone");
        link = rm.getData().get("link");

        if(link.contains("http://")){
            notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
        }else{
            notificationIntent = new Intent(FirebaseMessagingService.this, MainActivity.class);
        }

        if(phone.equalsIgnoreCase("")){
            iCall = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null));
            pintCall = PendingIntent.getActivity(FirebaseMessagingService.this, 0, iCall, PendingIntent.FLAG_UPDATE_CURRENT);
        }

    }

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
    PendingIntent pint = PendingIntent.getActivity(FirebaseMessagingService.this, 0, notificationIntent, 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(body)
            .setWhen(time)
            .setPriority(Notification.PRIORITY_MAX)
            .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
            .setContentIntent(pint);


    NotificationCompat.BigTextStyle bigtext =  new NotificationCompat.BigTextStyle();

    bigtext.setBigContentTitle(title);
    bigtext.bigText(body);

    mBuilder.setStyle(bigtext);

    if(!phone.equalsIgnoreCase("")){
        mBuilder.addAction(android.R.drawable.ic_menu_call, "Call me!", pintCall);
    }

    notificationManager.notify(0, mBuilder.build());
}

有人知道如何做到这一点吗?

谢谢。

Firebase 通知控制台发送所谓的 "notification messages"。当您的应用程序不在前台时,这些通知消息由系统本身处理,并以您看到的格式自动显示在通知托盘中。当用户点击这个通知时,应用程序被激活。

为确保消息始终传送到您的 onReceiveMessage 方法,请使用 "data messages".

有关详细说明,请参阅 documentation on the different message types