【问题标题】:How to set the app icon as the notification icon in the notification drawer如何将应用程序图标设置为通知抽屉中的通知图标
【发布时间】:2015-12-11 14:26:25
【问题描述】:

如图...
我收到通知图标(在红色左侧)。
但我需要显示应用程序图标,如黑色箭头所示

    public void notify(View view){
    notification.setSmallIcon(R.drawable.ic_stat_name);
    notification.setTicker("Welcome to ****");
    notification.setWhen(System.currentTimeMillis());
    notification.setContentTitle("abcd");
    notification.setContentText("abcd");


    Intent intent = new Intent(this, home.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setContentIntent(pendingIntent);


    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.notify(uniqueID, notification.build());
}

【问题讨论】:

  • 在通知生成器中使用 .setSmallIcon(R.drawable.ic_launcher)
  • 我已经发布了我的代码
  • @Sathish 不正确。 ic_launcher 不是可绘制的。使用setSmallIcon(R.mipmap.ic_launcher)

标签: android android-studio notifications android-notifications android-notification-bar


【解决方案1】:

我知道在这里回答有点晚了,而且它已经得到了回答,但我来这里是为了寻找一个使用 firebase 通知的简单解决方法。像我这样访问这里的人可以通过推荐且简单的 firebase 通知方式来解决此问题,即在清单中添加元数据。

Reference

<!-- Set custom default icon. This is used when no icon is set for incoming notification messages. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_stat_ic_notification" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming notification message. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent" />

【讨论】:

  • 您的回答是最有帮助的。谢谢老兄。
  • 你如何使这个动态化?
  • 此声明也用于 firebase 默认通知处理(当应用程序处于终止状态时),您可以在应用程序通知中使用自定义或修改 firebase 通知。看过来:stackoverflow.com/questions/37711082/…
  • 帮我更改了推送通知Display Messages 类型的默认图标。
【解决方案2】:

在您的通知生成器中尝试此代码:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
                        R.mipmap.ic_launcher))
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

android.app.NotificationManager notificationManager =
                (android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

设置大图标可以解决问题。如果您有任何进一步的信息,请在下方评论

【讨论】:

  • 好,添加这一行:.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
  • 如果应用不在前台怎么办
  • 大图标和小图标都显示,有什么办法可以去掉小图标
  • 我看不出这段代码有什么问题,但有趣的是,这段代码一直在使用最新的 Android O 的 Nexus 5X 中使系统 UI 崩溃。起初,我认为我的应用程序有问题工作,但后来我创建了一个新的应用程序,又是同样的事情。这看起来像是一个主要的 Android 操作系统问题。
  • 2019年不工作。帮助大图标仍然是灰色圆圈,白色透明小图标
【解决方案3】:

在构建通知时设置此参数

setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_launcher))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    相关资源
    最近更新 更多