Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通知未在Android Studio中显示_Android_Android Studio_Kotlin - Fatal编程技术网

通知未在Android Studio中显示

通知未在Android Studio中显示,android,android-studio,kotlin,Android,Android Studio,Kotlin,我正在使用Kotlin和Android Studio尝试在测试应用程序中推送通知。我按照Android开发者网站上关于如何创建通知()的说明进行了操作,但我似乎做错了什么,因为我的通知没有显示在任何地方。这是我的密码: val intent = Intent(context, Profile::class.java) val pendingIntent = PendingIntent.getActivity(this.context, 0, intent, 0);

我正在使用Kotlin和Android Studio尝试在测试应用程序中推送通知。我按照Android开发者网站上关于如何创建通知()的说明进行了操作,但我似乎做错了什么,因为我的通知没有显示在任何地方。这是我的密码:

 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())

我真的不知道我错过了什么,所以如果有任何帮助,我将不胜感激

尝试此方法,您可以动态传递消息和标题:

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())
}

根据您的代码,您没有创建通知频道

通知通道必须来自Android Oreo及更高版本

因此,如果您在没有通知频道的情况下运行应用程序Android O及以上设备,您的通知将不会显示

创建通知通道 然后,在创建通知时使用相同的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())
希望有帮助。

试试类似的方法

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);
不应该有分号。科廷不需要这个。
Profile是您的类还是系统类?

您好!感谢您的回答,但这不起作用。尝试将此字符串中的notificationId更改为非零值:notificationManager.notify(0,notificationBuilder.build())希望您已更改Notifications的id感谢您的回答,尝试过它,但仍然不起作用@easy\u breezy你的意思是我应该确保没有两个id相同的通知@haresh?欢迎使用堆栈溢出。你应该发布你在问题中提到的网站的链接。我们会调查的。谢谢
 val pendingIntent = PendingIntent.getActivity(this.context, 0, intent, 0);