push alarm message in android
2024-04-17 13:45

I'm developing an Android app and I have a question.

I receive an alarm while developing a push alarm, but I want to customize the icon as shown in the image below. What should I do?

enter image description here

my app.... enter image description here

my code...

private void sendNotification(String title, String body) { NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription(CHANNEL_DESCRIPTION); channel.enableLights(true); channel.setLightColor(Color.BLUE); channel.enableVibration(true); notificationManager.createNotificationChannel(channel); } // ??? ???? ? ??? ???? ?? Intent ?? Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // ??? ???? ??? ?? ???? ??? ????? ?? PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE ); // Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.todaysun_logo_notification); // int newWidth = 100; // ??? ??? ???? ? // int newHeight = 100; // ??? ??? ???? ?? // boolean filter = true; // ???? ???? ?? ?? // Bitmap scaledLargeIcon = Bitmap.createScaledBitmap(largeIcon, newWidth, newHeight, filter); // Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.todaysun_logo); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.todaysun_notification2) .setContentTitle(Html.fromHtml("< b>" + title+ "< /b>")) .setContentText(body) .setAutoCancel(true) .setContentIntent(pendingIntent) .setStyle(new NotificationCompat.BigTextStyle().bigText(body)) // ?? ???? ... ?? ?? ?? ? ??? ?? .setColor(Color.BLUE) // .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(largeIcon).bigLargeIcon(null)) .setLargeIcon(largeIcon); ; notificationManager.notify(0, notificationBuilder.build()); }

enter image description here




other answer :

To customize the icon shown in your notification, you need to set the small icon for your NotificationCompat.Builder object. Heres how you can modify your sendNotification method to include a custom small icon:

javaprivate void sendNotification(String title, String body) { NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // Set your custom small icon here int smallIcon = R.drawable.your_custom_icon; // Replace your_custom_icon with the name of your custom icon resource if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription(CHANNEL_DESCRIPTION); channel.enableLights(true); channel.setLightColor(Color.BLUE); channel.enableVibration(true); notificationManager.createNotificationChannel(channel); } Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE); // Set your custom small icon here NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(smallIcon) // Set the custom small icon .setContentTitle(title) .setContentText(body) .setAutoCancel(true) .setContentIntent(pendingIntent); notificationManager.notify(0, notificationBuilder.build()); }

Replace your_custom_icon with the name of the drawable resource for your custom icon. Make sure to place the custom icon in the appropriate drawable folder of your Android project.

This modification will set your custom icon as the small icon for your notification, as shown in the image you provided.