我的通知在我点击按钮时没有出现。

huangapple go评论72阅读模式
英文:

My notification doesn't appear when I click in my button

问题

使用这段代码,我想要点击一个按钮然后出现一个通知。
问题是,当我点击我的按钮时,没有出现任何通知。
我在YouTube上搜索了一下,在示例代码中也找不到我的错误,你能看到吗?


btNotify.setOnClickListener(new View.OnClickListener() {
@Override
    public void onClick(View v) {
        notification();
    }
});


public void notification() {
        NotificationCompat.Builder builder = new 
        NotificationCompat.Builder(this);
        builder.setSmallIcon(R.drawable.ic_baseline_access_time_24);
        builder.setContentTitle("时间");
        builder.setContentText("这是一个时钟");

        NotificationManager notifManager = (NotificationManager) 
        getSystemService(Context.NOTIFICATION_SERVICE);
        notifManager.notify(1, builder.build());
}
英文:

With this code, I want to click in a button and appear a notification.
The problem is when in click in my button doesn't appear any notification.
I search in youtube, in example codes and I don't see my error, can you see??


btNotify.setOnClickListener(new View.OnClickListener() {
@Override
    public void onClick(View v) {
        notification();
    }
});


public void notification() {
        NotificationCompat.Builder builder = new 
        NotificationCompat.Builder(this);
        builder.setSmallIcon(R.drawable.ic_baseline_access_time_24);
        builder.setContentTitle("Time");
        builder.setContentText("Isto é um relógio");

        NotificationManager notifManager = (NotificationManager) 
        getSystemService(Context.NOTIFICATION_SERVICE);
        notifManager.notify(1, builder.build());
}

答案1

得分: 1

您没有提到您正在使用的 Android 版本。
根据官方文档,在使用 API 级别 26+(Android 8 或更高版本)时,您必须在构造器中为生成器提供一个通道。

var builder = NotificationCompat.Builder(this, CHANNEL_ID)

建议在应用程序启动时立即创建通道,因为如果没有通道,您无法发布通知(同样,这适用于 API 级别 26+)。不要忘记为通道分配一个优先级。有关创建通道的详细信息,请查看此处

无论如何,通常在应用程序不在使用时使用通知向用户提供信息。如果您只是想要在按下按钮后立即显示通知,也可以考虑使用 Toast 或 snackbar。可以像这样简单使用:

Toast.makeText(yourContext, "您的消息", Toast.LENGTH_LONG).show()

更新 2020/08/11

根据您提到的 Android 版本进行更新。

Android 文档如下所述:

请注意,NotificationChannel 构造函数需要一个重要性参数,使用 NotificationManager 类的常量之一。此参数确定如何为属于此通道的任何通知中断用户,尽管您还必须使用 setPriority() 来设置优先级,以支持 Android 7.1 及更低版本(如上所示)。

  1. 通过使用 NotificationCompat 构建器,您可以通过创建一个 通道 并将其提供给 NotificationCompat 构造器来实现与 Android 8+ 的向上兼容性。对于较低版本,通道将被简单地忽略。
  2. 严格要求设置通知的优先级。对于版本低于或等于 Android 7.1 的支持,您需要在生成器中设置优先级,因为通道(本身具有优先级设置)将被忽略。

尝试在生成器中添加必需的重要性规范以使用 setPriority:

val builder = ...
    .setPriority(NotificationCompat.PRIORITY_DEFAULT) // 或其他优先级级别

更新 2020-08-17

如何为 API 级别 26+ 创建通道,如官方文档所示:

private fun createNotificationChannel() {
    // 只有在 API 26+ 时才创建通道,因为 NotificationChannel 类是新的,不包含在支持库中
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val name = getString(R.string.channel_name)
        val descriptionText = getString(R.string.channel_description)
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
            description = descriptionText
        }
        // 向系统注册通道
        val notificationManager: NotificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }
}
英文:

You didn't mention the android version you are working with.
According to the official documentation you have to provide a channel to the builder constructor when using API level 26+ (Android 8 or higher).

var builder = NotificationCompat.Builder(this, CHANNEL_ID)

It is recommended to create the channel right on start-up of the app since you can not post notifications without it (again, this is valid for API level 26+).
Don't forget to assign a priority to the channel.
You can find the details about creating a channel here.

Anyways, notifications are usually used for providing information to the user while the app is NOT in use.
If you just want an instant notification following a button press, could you also consider using a Toast or a snackbar?
It can be as simple as:

Toast.makeText(yourContext, "Your message", Toast.LENGTH_LONG).show()

Update 2020/08/11

Update according to the android version you mentioned.

The android documentation states the following:

> Notice that the NotificationChannel constructor requires an importance, using one of the constants from the NotificationManager class. This parameter determines how to interrupt the user for any notification that belongs to this channel—though you must also set the priority with setPriority() to support Android 7.1 and lower (as shown above).

  1. by using the NotificationCompat builder, you get upward compatibility with android 8+ by creating a channel and providing it to the NotificationCompat constructor. For lower versions the channel is simply ignored.
  2. A priority setting is strictly required for using notifications. For support with versions lower or equal to Android 7.1: you need to set the priority in the builder since the channel (which has a priority setting on its own) is ignored.

Try adding the required importance specification with setPriority in the builder:

val builder ...
    .setPriority(NotificationCompat.PRIORITY_DEFAULT) // or any other priority level 

Update 2020-08-17

How to create a Channel for API level 26+ as shown in the official documentation:

private fun createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val name = getString(R.string.channel_name)
        val descriptionText = getString(R.string.channel_description)
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
            description = descriptionText
        }
        // Register the channel with the system
        val notificationManager: NotificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }
}

huangapple
  • 本文由 发表于 2020年8月10日 22:51:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63342611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定