Firebase cloud messaging: how to set notification icon on Android?

I am having a problem installing a notification icon on Android Studio.

I set the drawable folder as follows:

enter image description here

And I also set the default icon in my AndroidManifest.xml file:

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

And here I set the icon field to notification_icon: https://developers.google.com/cloud-messaging/http-server-ref#downstream-http-messages-json (ps I know GCM, but it works. I get a notification with everything except the badge)

What am I missing? All I see is a white square inside a gray circle.

This is my internal code: Pushex.push(%{title: user.name, body: "message goes here", badge: 1, sound: "default", icon: "notification_icon"}, to: user.fcm_token, using: :gcm)( https://github.com/tuvistavie/pushex )

+4
3

... . : fooobar.com/questions/34212/...

:

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

android/app/build.gradle...

defaultConfig {
    targetSdkVersion 20 // this has to be < 21
    ...
}

, .

+2

. .

 public class MyFirebaseMessagingService extends FirebaseMessagingService {
            private static final String TAG = "FCM Service";

            @Override
            public void onMessageReceived(RemoteMessage remoteMessage) {       
                Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                            Intent intent = new Intent(getApplicationContext(), YourClass.class);
                    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), NotificationID.getID(), intent,
                            PendingIntent.FLAG_UPDATE_CURRENT);
                    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
                            .setSmallIcon(getNotificationIcon())
                            .setContentTitle(remoteMessage.getData().get("title"))
                            .setContentText(remoteMessage.getData().get("body"))
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getData().get("body")))
                            .setContentIntent(contentIntent);
                    NotificationManager notificationManager =
                            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    notificationManager.notify(NotificationID.getID(), notificationBuilder.build());
            }
            private int getNotificationIcon() {
                boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
                //If the build version is higher than kitkat we need to create Silhouette icon. 
                return useWhiteIcon ? R.mipmap.ic_notification : R.mipmap.ic_launcher;
            }

            public static class NotificationID {
                private static final AtomicInteger c = new AtomicInteger(0);

                public static int getID() {
                    return c.incrementAndGet();
                }
            }
        }

, kitkat, Silhouette. -       . https://romannurik.imtqy.com/AndroidAssetStudio/icons-notification.html

+1

Together with SDK 9.8.0 you can override the default value! In your AndroidManifest.xml, you can set the following fields for setting the icon and color:

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

and

<manifest>
<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name">

    <!-- Add your meta-data here-->

    <activity 
        android:name=".MainActivity" 
        android:label="@string/app_name">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

</application>

0
source

Source: https://habr.com/ru/post/1680491/


All Articles