1

This question and related ones has been asked many times, but I'm asking for help in my specific trouble because I have reviewed the Firebase guides, and various questions/answers on how to add an icon to the push notification and can't seem to get it.

I believe I have a small mistake somewhere. I'm hoping you can glance my code and spot it.

Here's my app's AndroidManifest.xml.

<application
    android:allowBackup="false"
    android:hardwareAccelerated="true"
    android:icon="@mipmap/ic_launcher"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:label="@string/app_name"
    android:theme="@style/AppThemeFull">
    <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@mipmap/ic_launcher_round" />
    <meta-data android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/white" />

My mipmap/ic_launcher_round/ic_launcher_round.xml:

<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@mipmap/ic_launcher_background"/>
    <foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>

I icons listed on the left show up just fine in Android Studio if you double click each of them. (I feel there's probably more things to show which I'm not aware. Please add a comment if you're missing something and I'll promptly edit the question.)

enter image description here

Here's my push notification service:

package my.package;

import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import java.util.Map;

public class Notifications extends FirebaseMessagingService {
    private final String TAG = "XXX-PUSHNOTES";

    public Notifications() {
        Log.d(TAG, "instantiating class!");
    }

    private void sendMessage(Map<String, String> map) {
        Intent intent = new Intent("LOAD_APP_GOING_TO_URL");
        for (Map.Entry e : map.entrySet())  {
            Log.d(TAG, "Key: " + e.getKey() + " Value: " + e.getValue());
            intent.putExtra((String) e.getKey(), (String) e.getValue());
        }
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "push notes service created");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "push notes service destroyed");
    }
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        // TODO(developer): Handle FCM messages here.
        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
            Log.d(TAG, remoteMessage.getData().get("LOAD_APP_GOING_TO_URL"));
            sendMessage(remoteMessage.getData());

            if (/* Check if data needs to be processed by long running job */ true) {
                // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
                //scheduleJob();
            } else {
                // Handle message within 10 seconds
                Log.d(TAG, "Not handling the message.");
            }
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }
    }
}

Here's my main activity handling the broadcast from the service above:

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    final String TAG = "XXX-WA-BROADCAST";
    @Override
    public void onReceive(Context context, Intent intent) {
        // Extract data included in the Intent
        String LOAD_APP_GOING_TO_URL = intent.getStringExtra("LOAD_APP_GOING_TO_URL");
        String LOAD_APP_GOING_TO_URL_TOAST_MSG = intent.getStringExtra("LOAD_APP_GOING_TO_URL_TOAST_MSG");
        Log.d(TAG, LOAD_APP_GOING_TO_URL);
        Log.d(TAG, LOAD_APP_GOING_TO_URL_TOAST_MSG);
        Toast t = Toast.makeText(getApplicationContext(), LOAD_APP_GOING_TO_URL_TOAST_MSG, Toast.LENGTH_LONG);
        t.setGravity(Gravity.TOP, 0, 0);
        t.show();
    }
};
user9654148
  • 125
  • 7

1 Answers1

0

Which part of your code is ceating a notification? If I understand your code right, you are creating toast with the message on the top of the screen instead of real notifications?

You can create messages as you want:

 private Notification buildForegroundNotification(String message) {
    NotificationCompat.Builder b = new NotificationCompat.Builder(this, ChannelID);

    b.setOngoing(true)
            .setContentTitle("test")
            .setContentText(message)
            .setSmallIcon(1)//set there the Ressource id
            .setTicker("test");

    return (b.build());
}
Jones
  • 141
  • 1
  • 9
  • 1
    I'm creating a toast message when the app receives the notification while in foreground. When the app is not running or is in background, then it is the system tray that displays the push notification. It is the icon in the system tray that I'd like to change. How can tell the system to use my icon instead of Android's default icon --- like everybody else does? – user9654148 Sep 03 '19 at 16:02
  • @user9654148 Any solution? – Itay Feldman Jan 25 '21 at 11:54