我应该将什么 "channelId" 传递给 NotificationCompat.Builder 的构造函数?

What "channelId" should I pass to the constructor of NotificationCompat.Builder?

我正在尝试编写一个简单的通知应用程序。它所做的只是在单击按钮时发出通知。

我按照this教程一步一步写我的app。

我不明白我应该将什么频道 ID 传递给构造函数,以便应用程序正常运行。现在该应用程序不执行任何操作(它也没有崩溃)。我猜问题出在变量 NotificationCompat.Builder notifyBuilder 的初始化上。我不知道要传递什么,所以我只是传递了一个任意字符串 "0".

我的问题是我应该将什么传递给 NotificationCompat.Builder 的构造函数,它会让应用程序运行吗?

这是 MainActivity:

public class MainActivity extends AppCompatActivity {
    private Button mNotifyButton;
    private static final int NOTIFICATION_ID = 0;
    private static final String NOTIFICATION_ID_STRING = "0";
    private NotificationManager mNotifyManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNotifyButton = (Button) findViewById(R.id.notificationButton);
    }

    public void sendNotification(View view) {

        mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder notifyBuilder
                =new NotificationCompat.Builder(this, NOTIFICATION_ID_STRING)
                .setContentTitle("You've been notified!")
                .setContentText("This is your notification text.")
                .setSmallIcon(R.drawable.ic_android);
        Notification myNotification = notifyBuilder.build();
        mNotifyManager.notify(NOTIFICATION_ID, myNotification);

    }
}
private static final int NOTIFICATION_ID = 0;

这个标识您的通知。如果您发送多个具有相同 ID 的通知,则旧通知将被新通知替换。所以,如果想一次显示多个通知,你需要多个ID。

private static final String NOTIFICATION_ID_STRING = "0";

这是频道的ID。它告诉应该使用哪个通道来发送该通知。您可以分配任何 String。您只需要确保对特定频道使用相同的 String。如果您想创建更多频道,则需要为它们使用不同的 String

关于频道

在 Android 的最新版本中(Android Oreo 之后),您可以使用不同的渠道来发送通知。这样,您就可以让您的用户自定义他想要接收的通知。

例如,您有一个购物应用程序。然后,您可以拥有一些频道,例如:

  • 我的订单(通知订单)
  • 促销(通知有关促销的信息.. 用户可能会想要禁用)。
  • 杂项(任何其他类型的通知)。

在该频道上发送通知之前,您应该创建它:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    CharSequence name = "Orders Notification";
    String description = "Notifications about my order";
    int importance = NotificationManager.IMPORTANCE_DEFAULT;

    NotificationChannel channel = new NotificationChannel(NOTIFICATION_ID_STRING, name, importance);
    channel.setDescription(description);

    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(channel);
}

然后,您可以通过以下方式发送通知:

NotificationCompat.Builder notifyBuilder
            = new NotificationCompat.Builder(this, name.toString())

您可以通过以下方式查看应用程序拥有的所有频道:

Settings -> Apps -> App you want to check -> Notifications

有些应用只有一个频道。其他应用程序可能有多个频道。

回到你的问题

所以,回到你的例子,NOTIFICATION_ID_STRING 可以是任何字符串。每次您想在该特定频道上发送消息时,只需使用 "0" 即可。 并且,如果您创建新频道,请使用新字符串 "channel1",例如。

但是,您必须在发送通知之前创建一个频道:

// This ID can be the value you want.
private static final int NOTIFICATION_ID = 0;

// This ID can be the value you want.
private static final String NOTIFICATION_ID_STRING = "My Notifications";

public void sendNotification(View view) {

    mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    //Create the channel. Android will automatically check if the channel already exists
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(NOTIFICATION_ID_STRING, "My Channel Name", NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription("My notification channel description");
        mNotifyManager.createNotificationChannel(channel);
    }

    NotificationCompat.Builder notifyBuilder
            = new NotificationCompat.Builder(this, NOTIFICATION_ID_STRING)
            .setContentTitle("You've been notified!")
            .setContentText("This is your notification text.")
            .setSmallIcon(R.drawable.ic_android);
    Notification myNotification = notifyBuilder.build();
    mNotifyManager.notify(NOTIFICATION_ID, myNotification);
}

您可以找到有关它们的更多信息HERE