0

When I open the app and receive the notification, my custom notification shows.
But when the app is in the background, it just uses the default notification setting. I'm trying to change the icon of the notification from that dot to my app logo.Notification with a dot

My FirebaseMessagingService code is below

public static final String TG = "FCMService";
NotificationManagerCompat notificationManager;

@Override
public void onNewToken(@NonNull String s) {
    Log.i(TG, "The token refreshed: " + s);
    super.onNewToken(s);
}

@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    if(remoteMessage.getData().size() > 0)
    {
        Log.d(TG, "Message data payload : " + remoteMessage.getData());
    }

    if(remoteMessage.getNotification() != null)
    {
        Log.d(TG, "Message notification body : " + remoteMessage.getNotification().getBody());
    }

    Log.d(TG, "From : " + remoteMessage.getFrom());

    Map<String, String> s =  remoteMessage.getData();

    sendNotification(s.get("message"));


}

private void sendNotification(String message) {
    notificationManager = NotificationManagerCompat.from(this);

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 ,intent, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
            .setSmallIcon(R.drawable.ic_07c1b5098af03f95f3c3e8f7d461fb78)
            .setContentTitle("Just In")
            .setContentText(message)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .build();
    notificationManager.notify(1, notification);
}

And my Manifest file is

<service
        android:name=".MyFirebaseMessagingService"
        android:exported="true"
        android:enabled="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
</service>
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
z4rtx
  • 127
  • 1
  • 8
  • 1
    Does this answer your question? [Firebase onMessageReceived not called when app in background](https://stackoverflow.com/questions/37358462/firebase-onmessagereceived-not-called-when-app-in-background) – AgentP Jul 14 '20 at 10:24
  • yes, but no. I want to show my app logo icon when the notification comes in when app is in background. And also set a click listener so instead of opening a main activity, i want it to open a specific activity – z4rtx Jul 14 '20 at 10:33
  • Yeah the question contains answer to that as well .. check all answers – AgentP Jul 14 '20 at 10:34
  • i hve seen wht happened, will explain all that in a bit, but now, my main issue, can i change the notification icon? if the app is in background. – z4rtx Jul 14 '20 at 10:44
  • So alternative i found fro opening a specific activity instead of main activity, is to open the main activity and see what data was received using `getIntent.getExtras();`. Then from the MainActivity, i automatically navigate to the needed Activity to use the data from the Notification – z4rtx Jul 14 '20 at 11:04

2 Answers2

1

There are two types of messages data messages and notification messages. Data messages are handled here in onMessageReceived whether the app is in the foreground or background. Data messages are the type traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app is in the foreground. When the app is in the background an automatically generated notification is displayed. When the user taps on the notification they are returned to the app. Messages containing both notification and data payloads are treated as notification messages. The Firebase console always sends notification messages.

Example code :- You need to specify icon in your notification payload like this

$notification = array
    (
    'icon' => 'icon here',
    'title' => 'title',
    'body' => 'new msg',
    'click_action' => 'action here'
); 

Note:- you need to add these in manifest to use default notification icon like this

<meta-data
      android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_launcher" />

// optional if required
    <meta-data android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/notification_color" />
Quick learner
  • 10,632
  • 4
  • 45
  • 55
  • so if i'm using the firebase console to send test messages, i can't change icon from there. But what if im using a Wordpress Plugin to send notifications using the FCM? there is no spot for setting icon there... – z4rtx Jul 14 '20 at 10:50
  • updated my answer, please check note section and do the same in manifest file if not set and try it – Quick learner Jul 14 '20 at 10:56
  • i am glad it worked, please upvote my answer also so others can refer it – Quick learner Jul 14 '20 at 11:03
0

By the reference of Firebase, when your app is in the background, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.

Messages with both notification and data payload, when received in the background. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

Firebase Reference

Android Pie And Newer version has some limitations. Check this reference

Yabaze Cool
  • 523
  • 6
  • 16