3

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/

X Fa
  • 137
  • 2
  • 9

1 Answers1

7

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'
);
Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39
  • thanks, you mean I should use some server side service to send data , and I cant use firebase console to do that? – X Fa Jun 01 '18 at 12:19
  • @XFa FIrebase Console doesn't support `Data` only payload. Most easy way you can achieve this is [FCM HTTP Protocol](https://firebase.google.com/docs/cloud-messaging/http-server-ref) – wonsuc Jun 01 '18 at 13:04
  • thank you for your answer, but it is not a good way to change the server-side and has some effects on other platforms, is there any other way to show custom notification in the background when we receive both notification and data from server?! – Azin Nilchi Sep 20 '20 at 08:15
  • @AzinNilchi You are right we can't edit server code which one used with multiple platforms, But actually no other direct solution I suggested and another you can do one thing just put the condition on the server when you are going for android that time change just code for other put as it is. – Dhaval Solanki Sep 21 '20 at 06:02
  • thank you, I asked my problem again here : https://stackoverflow.com/q/63976951/4646999, but it seems there is no other way. – Azin Nilchi Sep 21 '20 at 09:11