简体   繁体   中英

Push notification foreground and background

I've created a simple notification and styled it with different colors and general style using a completely new layer created and designed.. Once I trigger the notification though Firebase Messages, when the app is at the foreground, it works perfectly showing the exact styling I need.

But when the app is in the background, it uses it's fugly default style.

Anyway to fix that? Thanks.

The notification java class file code -

public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

private static final String TAG = "FirebaseMessagingServic";

public MyFirebaseMessagingService() {
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    String title = remoteMessage.getNotification().getTitle();
    String message = remoteMessage.getNotification().getBody();
    Log.d(TAG, "onMessageReceived: Message Received! \n " +
        "Title : " + title + "\n" +
            "Message : " + message);

    sendNotification(title, message);

}

@Override
public void onDeletedMessages() {



}

private void sendNotification(String title, String messageBody) {

    final RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_layout);

    remoteViews.setTextViewText(R.id.remoteview_notification_short_message, messageBody);
    remoteViews.setTextViewText(R.id.notificationDate, title);

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = "0";
    Uri defaultSoundUri= Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/notice");
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)

                    .setSmallIcon(R.mipmap.minilogored)
                    .setContentIntent(pendingIntent)
                    .setContent(remoteViews)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setLights(0xf14d39, 1500, 2000)
                    .setPriority(NotificationCompat.PRIORITY_HIGH);


    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

}

Please refer the link to know more about Data message and Display message how to handle in onMessageReceived() callback function.

How to handle notification when app in background in Firebase

https://firebase.google.com/docs/cloud-messaging/android/receive

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM