推送通知Kotlin

时间:2019-07-27 04:08:27

标签: android kotlin

该如何解决? 有什么想法吗? 谢谢你的帮助

private var notificationManager: NotificationManager? = null

......

problem Classifier 'NotificationManager' does not have a companion object, and thus must be initialized here

代码行->

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

代码

override fun onChildAdded(ds: DataSnapshot, prevChildKey: String?) {

    Log.i(TAG, " child added")
    if(markers.size == 0) {
        pushIssueMarker(ds)
    }
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setContentTitle("Firebase Push Notification");
    builder.setContentText("Hello this is a test Firebase notification, a new database child has been added");
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager?.notify(1, builder.build());
}

2 个答案:

答案 0 :(得分:0)

您正在用Kotlin进行编码。因此其对象创建与JAVA不同。

更改您的下一行

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

此行

val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager

答案 1 :(得分:0)

您无需分成两行,可以轻松地将其初始化为

val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
相关问题