当应用程序处于后台时,如何显示Firebase通知的自定义UI? [英] How to show custom UI for firebase notification when the app is in background?

查看:255
本文介绍了当应用程序处于后台时,如何显示Firebase通知的自定义UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此类显示从firebase控制台收到的自己的UI(RemoteViews)的通知.当应用程序为前台应用程序时,此方法工作正常,但当应用程序为后台应用程序时,通知以默认的设备样式显示.即使应用程序为前台或背景,我该如何在自己的UI中显示通知?

I use this class to show notification whit my own UI (RemoteViews) , received from firebase console. This works fine when the app is foreground , but when the app is in background, notification displayed in default style of device.What should I do to show notification in my own UI even the app is foreground or background?

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/

Note : thanks for the correct answer, I could send notification with my own UI by using this link , even app is in background or foreground : http://www.androiddeft.com/2017/11/18/push-notification-android-firebase-php/

推荐答案

实际上是在发送通知时我们使用的两种类型的有效载荷,

Actually tow type of payload we are using when sending the notification,

一个是通知有效负载,另一个是数据有效负载. 通知有效负载自动管理通知,当您在前台时它们会从Firebase服务中调用onMessageReceived,但是当您在后台时它们不会调用onMessageReceived,

One is Notification Payload and another one is Data Payload. Notification payload manage notification automatically when you are in the foreground they call onMessageReceived from firebase service but when are you in Background they do not call onMessageReceived,

因此,出于解决方案的目的,只需在 Data 有效负载中发送数据并删除 Notification 有效负载,以便您可以在每种状态下的onMessageReceived中获取通知,并可以管理该UI

So for the solution purpose just send data in Data payload and remove Notification payload so You can get the notification in onMessageReceived in every state and you can manage UI of that.

查看下面的示例

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通知的自定义UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆