Zolbayar
Zolbayar

Reputation: 936

Showing big view notifications with Firebase Cloud Messaging

In the Firebase Cloud Messaging documentation, nothing mentioned about the notifications with big view/expanded layout.

How should I display big view notifications when app is background? Creating custom notification in FirebaseMessagingService's onMessageReceived seems not possible according to this faq:

When your app is in the background, notification messages are displayed in the system tray, and onMessageReceived is not called. For notification messages with a data payload, the notification message is displayed in the system tray, and the data that was included with the notification message can be retrieved from the intent launched when the user taps on the notification.

Upvotes: 1

Views: 841

Answers (1)

Brianvdb
Brianvdb

Reputation: 676

Send the notification you want to see using the data object. You can basicly put everything you want in the data object and always receive it at the onMessageReceived method. Here's an example.

public class AppFireBaseMessagingService extends FirebaseMessagingService {

    private final static int REQUEST_CODE = 1;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Map<String, String> data = remoteMessage.getData();
        if (data == null) return;

        if (data.containsKey("title") && data.containsKey("message")) {
            showNotification(data.get("title"), data.get("message"));
        }
    }

    private void showNotification(String title, String body) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setContentTitle(title)
                .setSmallIcon(R.drawable.notification_icon);

        if (body != null && !body.isEmpty()) {
            builder.setStyle(new NotificationCompat.BigTextStyle().bigText(body));
            builder.setContentText(body);
        }

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        builder.setContentIntent(contentIntent);
        builder.setAutoCancel(true);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification n = builder.build();
        n.defaults = Notification.DEFAULT_ALL;
        notificationManager.notify(0, n);
    }

}

Upvotes: 1

Related Questions