How to show custom UI for firebase notification when the app is in background?

雨燕双飞 提交于 2020-01-04 08:09:09

问题


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());
        }

    }

}

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/


回答1:


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

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,

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.

Check bellow example

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'
);


来源:https://stackoverflow.com/questions/50640957/how-to-show-custom-ui-for-firebase-notification-when-the-app-is-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!