最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - unable to get notification manager - Stack Overflow

programmeradmin3浏览0评论

I am attempting to create a notification service so that I can merely show notifications from a background service. I already have that wired up with Google Pub Sub and working. The trouble I am having is getting those messages turned into notifications and displayed. Once I have a primitive notification working, I can worry about details such as icons, color, actions, etc.

Then, in theory, inside of my service, I would call another function I have defined which publishes those notifications - I don't believe I have any concern here other than I need to create and set an icon rather than use an internal icon and hard-coded values.

Then, how do I get a notification manager properly?

I am following the documentation here:

It appears to reference Android 8, but I am targeting Android 14. In particular, the issue I have is with this line:

val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager

If I drill down into the source for the method, the current source I am seeing looks like this:

    @Nullable
public static <T> T getSystemService(@NonNull Context context, @NonNull Class<T> serviceClass) {

In my main activity, I have a private function which looks like:

private fun createNotificationChannel(context: Context): NotificationChannel {
val notificationChannel = NotificationChannel("channel-id", "notification-channel", NotificationManager.IMPORTANCE_DEFAULT).apply {
description = "notification channel"
    }

notificationChannel.enableVibration(true)

val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel)

return notificationChannel
}

Please be kind, I work in Android rarely though I code in Java daily and need nudged in the right direction.

EDIT: Is this what I need to do then? This was not obvious to me:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
发布评论

评论列表(0)

  1. 暂无评论