当应用程序处于后台时,如何显示Firebase通知的自定义UI?

3

我使用这个类来展示从Firebase控制台接收到的通知,使用自己的UI(RemoteViews)。当应用程序在前台时,这很好运作,但是当应用程序在后台时,通知会以设备默认样式显示。我该怎么做才能在应用程序在前台或后台时都展示自己的UI?

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "Mehdi";

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

        if (remoteMessage.getNotification() != null)
        {
            RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.payam_notification_layout);
            contentView.setTextViewText(R.id.payam,remoteMessage.getNotification().getBody());

            String channelId = "Default";
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this,channelId)
                            .setSmallIcon(R.drawable.status_icon_delivered)
                            .setLargeIcon(BitmapFactory.decodeResource( getResources(), R.mipmap.icon))
                            .setSound(Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.notification))
                            .setCustomBigContentView(contentView)
                            .setContentTitle(remoteMessage.getNotification().getTitle())
                            .setContentText(remoteMessage.getNotification().getBody())
                            .setAutoCancel(true);

            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
                manager.createNotificationChannel(channel);
            }
            manager.notify(0, mBuilder.build());
        }

    }

}

注意:感谢正确的答案,我可以使用此链接发送通知,并在应用程序处于后台或前台时使用自己的UI:http://www.androiddeft.com/2017/11/18/push-notification-android-firebase-php/

1个回答

7

我们在发送通知时使用了两种类型的负载,一种是通知负载,另一种是数据负载。
当您处于前台时,通知负载会自动管理通知,它们会从Firebase服务调用onMessageReceived。但是当您处于后台时,它们不会调用onMessageReceived。

因此,为了解决这个问题,只需将数据发送到数据负载中并删除通知负载,这样您就可以在任何状态下通过onMessageReceived获取通知,并且您可以管理它们的用户界面。
请查看以下示例:

function sendFCMNotification($message,$id) {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
        'to' => $id,
        'data' => array (
                "body" => $message,
                "title" => "Title Text"
        )
);
$fields = json_encode ( $fields );
$headers = array (
        'Authorization: key=' . "Legcy Key",
        'Content-Type: application/json'
);

谢谢,您的意思是我应该使用一些服务器端服务来发送数据,而不能使用Firebase控制台来完成吗? - X Fa
@XFa Firebase控制台不支持“Data”负载。最简单的方法是使用FCM HTTP协议来实现此功能。 - wonsuc
谢谢你的回答,但是改变服务器端并且对其他平台产生影响并不是一个好的解决方法。是否有其他方式在后台接收到通知和数据时显示自定义通知? - Azin Nilchi
@AzinNilchi,你说得对,我们不能编辑用于多个平台的服务器代码。但实际上我没有提出其他直接的解决方案。你可以在服务器上加入条件,当你使用安卓时改变代码,而其他平台则保持不变。 - Dhaval Solanki
谢谢,我在这里重新提出了我的问题:https://stackoverflow.com/q/63976951/4646999,但似乎没有其他办法。 - Azin Nilchi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接