web-dev-qa-db-fra.com

Android Icône de notification de couleur

Je travaille sur une application dans laquelle je crée une notification pour l'utilisateur. Je souhaite que l'icône apparaisse en blanc lorsqu'elle est dans la barre d'état, mais en bleu lorsqu'elle est affichée dans le menu de notification déroulant. Voici un exemple de la même chose réalisée par l'application Google Store.

Notification blanche dans la barre d'état:

enter image description here

Notification colorée dans le menu déroulant:

enter image description here

Comment puis-je reproduire cela? Quelles propriétés dois-je définir?

Éditer: Voici mon code actuel - J'ai rendu l'image entièrement blanche avec un arrière-plan transparent, de sorte que tout se passe bien dans la barre d'état , l’image est toujours de la même couleur blanche:

private NotificationCompat.Builder getNotificationBuilder() {
        return new NotificationCompat.Builder(mainActivity)
                .setDeleteIntent(deletedPendingIntent)
                .setContentIntent(startChatPendingIntent)
                .setAutoCancel(true)
                .setSmallIcon(R.drawable.skylight_notification)
                .setColor(ContextCompat.getColor(mainActivity, R.color.colorPrimary))
                .setContentTitle(mainActivity.getString(R.string.notification_title))
                .setContentText(mainActivity.getString(R.string.notification_Prompt));
    }
33
Oblivionkey3

J'ai trouvé la réponse à ma question ici: https://stackoverflow.com/a/44950197/4394594

Je ne sais pas vraiment quel était le problème, mais en plaçant le fichier png énorme que j'utilisais pour l'icône dans l'outil this https://romannurik.github.io/AndroidAssetStudio/icons-notification.html# source.type = image & source.space.trim = 1 & source.space.pad = 0 & name = ic_skylight_notification et en plaçant les icônes générées qu'il a données dans mon dossier mipmap, j'ai pu obtenir la propriété setColor(...) fonctionne correctement.

24
Oblivionkey3

Pour les notifications firebase envoyées depuis la console, il vous suffit de l’ajouter dans votre manifeste:

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

    <meta-data
        Android:name="com.google.firebase.messaging.default_notification_color"
        Android:resource="@color/custom_color" />

Où white_logo correspond au logo blanc de votre application et custom_color à la couleur pour laquelle l'icône et le texte doivent être colorés.

Plus de détails ici: https://firebase.google.com/docs/cloud-messaging/Android/client

13
radu_paun

Voici ce que j'ai fait pour mon application ...

private void showNotification(Context context) {
    Log.d(MainActivity.APP_TAG, "Displaying Notification");
    Intent activityIntent = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
    mBuilder.setSmallIcon(R.drawable.ic_notification);
    mBuilder.setColor(Color.GREEN);
    mBuilder.setContentIntent(pendingIntent);
    mBuilder.setContentTitle("EarthQuakeAlert");
    mBuilder.setContentText("It's been a while you have checked out earthquake data!");
    mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());
}

Échantillon avec couleur:

enter image description here

Échantillon sans couleur: enter image description here

13
JRG

Lors de la création de la notification, vous pouvez définir la couleur et l'icône. Si votre icône est une image en blanc pur, il appliquera la couleur pour vous aux endroits appropriés.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    val notificationId = 10 // Some unique id.

    // Creating a channel - required for O's notifications.
    val channel = NotificationChannel("my_channel_01",
            "Channel human readable title",
            NotificationManager.IMPORTANCE_DEFAULT)

    manager.createNotificationChannel(channel)

    // Building the notification.
    val builder = Notification.Builder(context, channel.id)
    builder.setContentTitle("Warning!")
    builder.setContentText("This is a bad notification!")
    builder.setSmallIcon(R.drawable.skull)
    builder.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
    builder.setChannelId(channel.id)

    // Posting the notification.
    manager.notify(notificationId, builder.build())
}
6
Advice-Dog

Si vous souhaitez modifier la couleur et le titre du titre selon Gmail et Twitter dans la notification Push ou la notification intégrée, vous devez ajouter ces lignes dans la notification.

 builder.setSmallIcon(R.drawable.skull)
    builder.setColor(ContextCompat.getColor(context, R.color.colorPrimary))

Première ligne utilisée pour l'icône et en deuxième ligne, vous devez définir la couleur

0
Pooja Kumari

Vous pouvez utiliser la DrawableCompat.setTint(int drawable); du dessinable avant de définir le dessinable. Et faites mutate() le dessinable, sinon la teinte de couleur sera appliquée à chaque instance de ce dessinable

0
Umar Hussain