通知在Android Studio中未显示

4

我正在使用Kotlin和Android Studio尝试在一个测试应用中推送通知。 我遵循了Android Developers网站上有关如何创建通知的说明 (https://developer.android.com/training/notify-user/build-notification),但似乎做错了什么,我的通知没有显示在任何地方。 这是我的代码:

 val intent = Intent(context, Profile::class.java)
            val pendingIntent = PendingIntent.getActivity(this.context, 0, intent, 0);
            val builder = NotificationCompat.Builder(this.context)
                .setSmallIcon(R.drawable.notification_icon)
                .setContentTitle("My notification")
                .setContentText("Hello World!")
                .setPriority(NotificationCompat.PRIORITY_MAX)
            builder.setContentIntent(pendingIntent).setAutoCancel(true)
            val mNotificationManager = message.context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            with(mNotificationManager) {
                notify(123, builder.build())

我真的不知道我缺少什么,所以任何帮助都将不胜感激。


欢迎来到 Stack Overflow。您应该在问题中发布您所引用的网站链接。 - Vardan Agarwal
3个回答

6
根据你的代码,你没有创建通知渠道(Notification channel)。
从Android Oreo及以上版本开始,通知渠道是必需的。
因此,如果你在运行Android O或更高版本的设备上没有创建通知渠道,你的通知将不会显示。
创建通知渠道。
fun createNotificationChannel() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channelId = "all_notifications" // You should create a String resource for this instead of storing in a variable
            val mChannel = NotificationChannel(
                channelId,
                "General Notifications",
                NotificationManager.IMPORTANCE_DEFAULT
            )
            mChannel.description = "This is default channel used for all other notifications"

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

在创建通知时,使用相同的 channelId 创建通知。

createNotificationChannel()
val channelId = "all_notifications" // Use same Channel ID
val intent = Intent(context, Profile::class.java)
val pendingIntent = PendingIntent.getActivity(this.context, 0, intent, 0);
val builder = NotificationCompat.Builder(this.context, channelId) // Create notification with channel Id
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("My notification")
    .setContentText("Hello World!")
    .setPriority(NotificationCompat.PRIORITY_MAX)
builder.setContentIntent(pendingIntent).setAutoCancel(true)
val mNotificationManager =
    message.context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
with(mNotificationManager) {
    notify(123, builder.build())

希望这可以帮到你。

会调查一下。谢谢! - user12656119

1
尝试类似以下的内容:
val intent = Intent(context, Profile::class.java)
val pendingIntent = PendingIntent.getActivity(this.context, 0, intent, 0)

val mNotificationManager =
    message.context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mNotificationManager

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//Don't forget this check

    val channel = NotificationChannel (
        channelId,
        "my_notification",
        NotificationManager.IMPORTANCE_HIGH
    )

    channel.enableLights(true)
    channel.lightColor = Color.GREEN
    channel.enableVibration(false)

    
    val builder = NotificationCompat.Builder(this.context, "channel_id")
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_MAX)
    builder.setContentIntent(pendingIntent).setAutoCancel(true)

    mNotificationManager.createNotificationChannel(channel)//Notice this
    mNotificationManager.notify(123, builder.build())
}

else {

    //Create Notifcation for below Oreo


}

你的 Logcat 是否显示类似以下内容:
E/NotificationManager: notifyAsUser: tag=null, id=123, user=UserHandle{0}

也请看一下您的代码行:

val intent = Intent(context, Profile::class.java)

如果其他方法都失败了,可以创建一个带有“Hello World”的测试活动。
然后将这行代码改为:
val intent = Intent(context, testActivity::class.java)

请记得将该活动添加到您的清单文件中

以下是需要添加的代码

 val pendingIntent = PendingIntent.getActivity(this.context, 0, intent, 0);

不应该有分号。Kotlin不需要这个。 Profile是你的类还是系统类?


0

尝试使用这种方法,您可以动态地传递消息和标题:

private fun showNotification(title: String?, body: String?) {
    val intent = Intent(this, Profile::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    val pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT)

    val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
    val notificationBuilder = NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true)
            .setSound(soundUri)
            .setContentIntent(pendingIntent)

    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.notify(0, notificationBuilder.build())
}

嗨!谢谢你的回答,但这个也不起作用。 - user12656119
尝试将notificationId更改为正的非零值,以便在此字符串中使用:notificationManager.notify(0, notificationBuilder.build())。 - easy_breezy
希望你已经更改了通知的ID。 - haresh
谢谢回答,我尝试了但还是不行 @easy_breezy - user12656119
你的意思是我应该确保我没有两个具有相同ID的通知,@haresh? - user12656119
这是我代码中唯一的通知,因此同一ID被用于两次通知不应该成为问题。@haresh - user12656119

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接