带有 NotificationCompat.Builder 的 Android 状态栏通知.正确的实施方式



我一直在阅读有关如何正确执行状态栏通知的信息,但大多数解决方案都使用已弃用的函数。我认为NotificationCompat.Builder是解决我问题的最佳和最新解决方案。任何人都可以给我一个示例代码吗?

对于像我这样的新手来说,我发现的最好的例子似乎不清楚。我不知道我是否必须在函数中实现代码并调用它,或者我是否必须将代码粘贴到活动区域中。

Notification with NotificationCompat.Builder

提前感谢您的帮助和时间。

最后,

以下是创建简单通知的方法:

public void createNotification() {

        // Build notification
        // Actions are just fake
        Notification noti = new Notification.Builder(this)
            .setContentTitle("Advice")
            .setContentText("blahblahblahblah.").setSmallIcon(R.drawable.icon).build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // hide the notification after its selected
        noti.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, noti);
      }

它抛出"调用需要 API 级别 11"错误,但通过右键单击项目 --> Android 工具 --> Clean Lint Marker 解决了这个问题。

最新更新