`NotificationCompat.Builder`已经被弃用。

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

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)

huangapple
  • 本文由 发表于 2020年7月31日 21:24:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63192733.html
匿名

发表评论

匿名网友

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

确定