Java 不推荐使用NotificationCompat.Builder
我尝试过使用Java 不推荐使用NotificationCompat.Builder,java,android,deprecated,Java,Android,Deprecated,我尝试过使用NotificationCompat.Builder(上下文上下文,字符串channelId),但是我在上下文上下文上遇到了更多的错误,我完全不知道如何在hot上添加一个``channelId`。创建一个通知频道并不难。 下面是一些代码: 您可以在应用程序启动时在MainActivity或应用程序类中执行该代码 创建通道id后,可以将其用于 NotificationCompat.Builder(上下文,字符串channelId) 编辑 通道ID是一个简单的字符串。 例如,您可以将其添
NotificationCompat.Builder(上下文上下文,字符串channelId)
,但是我在上下文上下文
上遇到了更多的错误,我完全不知道如何在hot上添加一个``channelId`。创建一个通知频道并不难。
下面是一些代码:
您可以在应用程序启动时在MainActivity或应用程序类中执行该代码
创建通道id后,可以将其用于
NotificationCompat.Builder(上下文,字符串channelId)
编辑
通道ID是一个简单的字符串。
例如,您可以将其添加到常量文件:
private void sendNotification(Context context, String dnsModel) {
Intent intentAction = new Intent(context, MainActivity.class);
intentAction.putExtra("dnsModel", dnsModel);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intentAction, PendingIntent.FLAG_ONE_SHOT);
notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.dns_changer_ico_inverse)
.setContentTitle(context.getString(R.string.service_ready))
.setContentIntent(pendingIntent)
.addAction(R.drawable.ic_vpn_lock_black_24dp, context.getString(R.string.turn_on), pendingIntent)
.setAutoCancel(true);
Notification notification = notificationBuilder.build();
notificationManager.notify(1903, notification);
}
您现在可以在创建通知频道时使用此常量,并且在调用
NotificationCompat.Builder(上下文上下文,字符串channelId)
时,请尝试像channelIdI那样使用任何字符串,并告诉我上下文上下文
应该有,或者)
请发布准确的错误消息。将您的channelId添加到NotificationCompat.Builder。channelId可以是任何字符串。只需创建一个包含channelId的新字符串。确保将channelId放在上下文之后。i、 e.NotificationCompat.Builder(上下文,channelId)它没有回答我的问题,即通道ID是一个必须创建的常量字符串,与发送通知时使用的字符串相同。@LuTz找到了它?像这样将代码添加到myMainActivity.java
,但是,它仍然没有在我的bootceiver.java
中看到它,在您的bootceiver.java中,您应该使用NotificationCompat.Builder(context,MainActivity.CHANNEL\u ID)。
public static final String CHANNEL_ID = "your.package.name.notificationChannelId"