如何禁用通知小图标?

6
如何禁用隐藏通知小图标?我知道它是必须的,但我想隐藏或删除小图标,只显示大图标。
Notification.Builder builder = new Notification.Builder(FcmIntentService.this).setSmallIcon(R.drawable.notification_small_icon_transparent);
NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(11, builder.build());

我希望有类似谷歌+通知的效果:

enter image description here


.setSmallIcon(android.R.color.transparent) - Arjun saini
Er. Arjun Saini,我已经尝试过了,但是它显示一个红色圆圈,里面是白色的颜色。 - Edalat Feizi
你能否尝试不设置任何setSmallIcon或不在代码中使用它? - Arjun saini
如果您不设置小图标,通知将不会显示。小图标是必需的。 - Edalat Feizi
3个回答

4
你可以用这种方式隐藏。
.setSmallIcon(android.R.color.transparent)

4
它不会隐藏图标,而是在Lollipop系统上显示一个灰色的空圆圈。 - M. Marmor

2

创建自定义通知并从通知构建器中删除.setStyle()

在查看代码之前,请阅读此文档。非常有帮助,可以消除您的所有疑虑:Android自定义通知文档

使用提供的代码,在您的布局文件夹中只需创建两个布局,一个用于折叠视图,另一个用于展开视图(如果您想显示展开视图)。

// custom notification class
public class NotificationUtils {

private static String TAG = NotificationUtils.class.getSimpleName();
private String channelId = "notification_channel";
private static final int NOTIFICATION_ID_BIG_IMAGE = 101;

private Context mContext;

public NotificationUtils(Context mContext) {
    this.mContext = mContext;
}
//Call this method in your FirebaseMessagingService class
public void showNotificationMessage(String billUrl, String title, String message, String timeStamp, Intent intent) {
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    final PendingIntent resultPendingIntent =
            PendingIntent.getActivity(mContext,0,intent,
                    PendingIntent.FLAG_ONE_SHOT
            );
    final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext, channelId);

        if (billUrl != null && billUrl.length() > 4 && Patterns.WEB_URL.matcher(billUrl).matches()) {
            Bitmap bitmap = getBitmapFromURL(billUrl);
            if (bitmap != null) {
                showNotification(bitmap, mBuilder, title, message, timeStamp, resultPendingIntent);
            }
    }
}

private void showNotification(Bitmap bitmap, NotificationCompat.Builder mBuilder, String brandName,
                              String description, String timeStamp, PendingIntent resultPendingIntent) {

    // Get the layouts to use in the custom notification
    RemoteViews notificationLayout = new RemoteViews(mContext.getPackageName(), R.layout.view_collapsed_notification);
    RemoteViews notificationLayoutExpanded = new RemoteViews(mContext.getPackageName(), R.layout.view_expanded_notification);


    //Null and Empty checks for your Key Value Pairs
    if (bitmap != null) {
        notificationLayoutExpanded.setImageViewBitmap(R.id.bill_container, bitmap);
    }

    if (brandName != null) {
        notificationLayout.setTextViewText(R.id.content_title, brandName);
        notificationLayoutExpanded.setTextViewText(R.id.expand_content_title, brandName);
    }

    if (description != null) {
        notificationLayout.setTextViewText(R.id.content_text, description);
        notificationLayoutExpanded.setTextViewText(R.id.expand_content_text, description);
    }

    if (timeStamp != null) {
        notificationLayout.setTextViewText(R.id.timestamp, timeStamp);
        notificationLayoutExpanded.setTextViewText(R.id.expand_timestamp, timeStamp);
    }

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    mBuilder =
            new NotificationCompat.Builder(mContext, channelId)
                    .setSmallIcon(R.drawable.ic_notification_logo)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setCustomContentView(notificationLayout)
                    .setCustomBigContentView(notificationLayoutExpanded)
                    .setPriority(Notification.PRIORITY_MAX)
                    .setContentIntent(resultPendingIntent);
    NotificationManager notificationManager =
            (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "notifications",
                NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(NOTIFICATION_ID_BIG_IMAGE, mBuilder.build());

}


 // Downloading push notification image before displaying it in the notification tray
private Bitmap getBitmapFromURL(String strURL) {
    try {
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
}

Output of the code


-3

只需使用.setLargeIcon(getBitmap(R.drawable.large_icon)),对于.setSmallIcon()则使用透明图像。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接