java - Android 通知永远不会显示

标签 java android android-studio kotlin android-notifications

我正在制作一个带有通知的简单提醒应用程序,正如标题所示,我的 Android 通知永远不会显示。

我做了一个像这样的广播类:

class ReminderBroadcast : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
            var builder = NotificationCompat.Builder(context, "notifyUser")
                    .setSmallIcon(R.drawable.ic_notification)
                    .setContentTitle("Memo reminder")
                    .setContentText("You have a memo set to the current time!")
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)

            var notificationManager = NotificationManagerCompat.from(context)

            notificationManager.notify( 200, builder.build())

    }
}

在我的 MainActivity 中,我使用以下方法创建一个 channel :

private fun createNotificationChannel(){

            val name: CharSequence = "UserNotificationChannel"

            val description: String = "Channel for notifying users"

            val importance: Int = NotificationManager.IMPORTANCE_DEFAULT

            val channel: NotificationChannel = NotificationChannel("notifyUser", name, importance)
            channel.description = description

            val notificationManager = getSystemService(NotificationManager::class.java)

            notificationManager.createNotificationChannel(channel)

       // }

    }

最后,我在包含新提醒表单的 fragment 中创建通知。创建表单(用于提交按钮的 onClickListener)后,我创建通知,如下所示:

val intent = Intent(context, ReminderBroadcast::class.java)
val pendingIntent: PendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0)

val alarmManager: AlarmManager = requireContext().getSystemService(ALARM_SERVICE) as AlarmManager

//set notification to trigger 5 seconds from now
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5000, pendingIntent)

//set direction
findNavController().popBackStack()

通知从未显示,我已经好几天不知道自己错过了什么。预先感谢您的帮助:)

最佳答案

您没有收到通知的原因是通知 channel :-

从 Android 8.0(API 级别 26)开始,所有通知都必须分配给一个 channel 。对于每个 channel ,您可以设置应用于该 channel 中所有通知的视觉和听觉行为。然后,用户可以更改这些设置并决定应用程序中的哪些通知 channel 应该是侵入性的或完全可见的。

解决方案

第 1 步:创建通知 channel -

     object NotificationHelper {
    
      fun createNotificationChannel(context: Context, importance: Int, showBadge: Boolean, name: String, description: String) {
    
    
        // 1
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
          // 2
          val channelId = "${context.packageName}-$name"
          val channel = NotificationChannel(channelId, name, importance)
          channel.description = description
          channel.setShowBadge(showBadge)
    
          // 3
          val notificationManager = context.getSystemService(NotificationManager::class.java)
          notificationManager.createNotificationChannel(channel)
        }
      }
    }

第 2 步:在您的应用程序类中注册 channel

class AppClass : Application() {

override fun onCreate() {
    super.onCreate()

NotificationHelper.createNotificationChannel(this,
  NotificationManagerCompat.IMPORTANCE_DEFAULT, false,
  getString(R.string.app_name), "App notification channel.")

  }
}

步骤 3. 在 Manifest 中注册此应用程序类

 <application
    android:name=".AppClass"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

注意:通过此链接了解有关通知 channel 的更多信息:https://developer.android.com/training/notify-user/channels

关于java - Android 通知永远不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67427788/

相关文章:

android - 对基础 Activity 和顶级 Activity 的混淆

android - 如何从另一台电脑导出 android studio 证书或 keystore

java - 如何从 intellij idea 项目生成可执行文件?

java - 使特定 ListView 行背景不同颜色 - Android?

android - 长按 EditText 崩溃 : Unable to add window

android - 使用 Android Studio 生成未签名的发布 APK

java - Android Studio : Unable to resolve Symbol 'requestLocationUpdates'

java - 使用 Class<?> 变量创建通用对象

java - 从 jbutton 多次单击,然后填充表

javascript - 在 Android 中通过 Javascript 访问加速度计?