英文:
NotificationCompat.Builder is deprecated
问题
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);
}
英文:
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);
}
I've tried using NotificationCompat.Builder(Context context, String channelId)
but I'm getting more errors on Context context
and I have absolutely no ideea on hot to add a ``channelId`.
答案1
得分: 1
创建通知通道并不那么难。
以下是一些代码:https://developer.android.com/training/notify-user/channels#CreateChannel
您可以在应用程序启动时在您的MainActivity或Application类中执行此代码。
创建通道ID后,您可以将其与NotificationCompat.Builder(Context context, String channelId)
一起使用。
编辑
CHANNEL_ID是一个简单的字符串。
例如,您可以将其添加到常量文件中:
public static final String CHANNEL_ID = "your.package.name.notificationChannelId";
现在您可以在创建通知通道时和调用NotificationCompat.Builder(Context context, String channelId)
时使用这个常量。
英文:
Creating a notification channel is not that hard.
Here is some code: https://developer.android.com/training/notify-user/channels#CreateChannel
You can execute that code in your MainActivity or your Application class on app startup.
Once you created the channel id, you can use it with
NotificationCompat.Builder(Context context, String channelId)
EDIT
The CHANNEL_ID is a simple String.
You can for example add this to a constants file:
public static final String CHANNEL_ID = "your.package.name.notificationChannelId"
You can now use this constants hwen creating your notification channel and when calling NotificationCompat.Builder(Context context, String channelId)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论