【问题标题】:error: constructor Builder in class Builder cannot be applied to given types错误:类 Builder 中的构造函数 Builder 不能应用于给定类型
【发布时间】:2019-07-15 13:12:08
【问题描述】:

这是我的代码: 在这段代码中显示错误

getString(R.string.default_notification_channel_id))

这是完整的代码:

 public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    String messageTitle = remoteMessage.getNotification().getTitle();
    String messageBody = remoteMessage.getNotification().getBody();

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

    int mNotificationId = (int) System.currentTimeMillis();
    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mNotifyMgr.notify(mNotificationId,mBuilder.build());
}
}

【问题讨论】:

  • 您在 logcat 中看到的内容发布。
  • 这个字符串default_notification_channel_id是否真的存在于你的字符串文件夹中...
  • MyNewProjectHello空白片段Supreme_CH_01
  • 请不要滥用 cmets 发布代码。改为编辑您的问题并在此处添加信息。
  • 您的支持库版本是多少?

标签: android builder build-script


【解决方案1】:

使用 Notification 而不是 NotificationCompact 例如:

Notification.Builder mBuilder = new Notification.Builder(this, getString(R.string.default_notification_channel_id))

【讨论】:

    【解决方案2】:

    从文档https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html 我可以看到new NotificationCompat.Buildercontextstring 作为参数,你给thisStringthis 引用com.google.firebase.messaging.FirebaseMessagingService 而不是@ 987654330@ 在你的情况下。

    【讨论】:

    • 你是如何使用上下文的?
    • 私有上下文上下文;和 (context,getString(R.string.default_notification_channel_id))
    • 是的,但是context 指的是什么?
    • 没问题。是的,谷歌与他们的命名非常一致,所以名称中带有Service 的任何东西通常都会扩展Service,即Context。 OP 的问题很可能是在引入Notification 通道之前,他们使用的是旧版本的支持库,因此只有一个仅采用Context 的构造函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 2019-10-05
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多