【问题标题】:How to add application icon in notification tray?如何在通知托盘中添加应用程序图标?
【发布时间】:2023-03-12 11:32:01
【问题描述】:

我知道这是可能的。至少我看到各种应用程序曾经将它们的图标放在通知托盘中(例如 Skype)。

我的申请呢?我应该怎么做才能将我的图标或消息放在通知栏中?

【问题讨论】:

  • 只有在您有具体理由的情况下才这样做(例如,您正在通知他们一些背景事件,您正在使用startForeground() 从服务中提供持续的价值)。不要仅仅为了笑而提出Notification
  • 对于像我这样的桌面新手,“托盘图标”==“状态栏”:

标签: android notifications tray


【解决方案1】:

Documentation.

您可以在 NotificationCompat.Builder 对象中指定通知的 UI 信息和操作。要创建通知本身,请调用NotificationCompat.Builder.build(),它会返回一个包含您的规范的Notification 对象。要发出通知,您可以通过调用NotificationManager.notify()Notification 对象传递给系统...

【讨论】:

    【解决方案2】:

    这是一个显示通知的好例子。

    Notification Demo

    【讨论】:

      【解决方案3】:

      这是将通知消息放入通知托盘的代码。100% 工作。

         NotificationManager notificationManager = (NotificationManager) 
                        getSystemService(NOTIFICATION_SERVICE); 
      
                Intent intent = new Intent(this, EndActivity.class);          
              PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
      
              // build notification
              // the addAction re-use the same intent to keep the example short
              Notification n  = new Notification.Builder(this)
                      .setContentTitle("My message")
                      .setContentText("Subject")
                      .setSmallIcon(R.drawable.MyApplogo)
                      .setContentIntent(pIntent)
                      .setAutoCancel(true)
                      .setStyle(new Notification.BigTextStyle().bigText(""+msg.get(3))).build();
                    //  .addAction(R.drawable.line, "", pIntent).build();
              n.flags |= Notification.FLAG_AUTO_CANCEL;
               notificationManager.notify(0, n); 
      

      【讨论】:

      • @我的荣幸。如果该应用对您有用,您可以点赞
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多