Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Push Notification, when app is closed I get a different style

I am using FCM for notification, where everything is working fine, but till app is open, once I kill(close) the app or in background, I get the Notification in default style, can anyone help me with setting this notification style when app is closed(or any other suggestion). Kindly help me with this, Thankyou in Advance

Here is my Code

 @Override
public void onMessageReceived(RemoteMessage remoteMessage) {


    String title = "";
    if (remoteMessage.getNotification().getTitle() != null){
        title = remoteMessage.getNotification().getTitle();
    }

    String message = "";
    if (remoteMessage.getNotification().getBody() != null){
        message = remoteMessage.getNotification().getBody();
    }

    Log.e("notification","recieved");


    sendNotification(title, message);

}



private void sendNotification(String title, String message) {

    Intent intent = new Intent(this, MainActivity2.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0/*Request code*/, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    int color=getResources().getColor(R.color.dot_dark_screen2);

    NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.account_outline)
            .setColor(color)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
            .setContentTitle(title)
            .setContentText(message)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setAutoCancel(true)
            .setSound(notificationSound)
            .setContentIntent(pendingIntent);


    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0 /*ID of notification*/, notifiBuilder.build());

enter image description here

like image 430
Rajiv Reddy Avatar asked Jun 27 '17 10:06

Rajiv Reddy


People also ask

Do push notifications work when app is closed?

Let's start with Android. The Android OS is designed to listen for push messages and upon receiving one, wake up the appropriate Android app to handle the push message, regardless of whether the app is closed or not.

Can you personalize push notifications?

To add an extra layer of personalization to an iOS or Android push campaign message, you can use geo-location based push notifications. There are two ways brands can send push notifications based on a user's location: Location-based notifications: App users receive these pushes based on data collected on the backend.

Are push notifications different from app notifications?

Push Notification vs In-App MessagePush notifications – standard mobile notifications that are used most often. These are messages that the user sees without opening the app, typically on the lock screen. In-app notifications – messages that the user gets inside the application after they have opened it.


1 Answers

I already posted a long explanation here: Android notification icon issue

TL;DR:

Most likely your problem is the difference between notification-messages and data-messages.

Please read: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want to process the messages on your client app.

like image 96
Diego Giorgini Avatar answered Oct 13 '22 06:10

Diego Giorgini