【问题标题】:Android notification never shows upAndroid 通知永远不会出现
【发布时间】:2021-05-07 01:14:58
【问题描述】:

我正在制作一个带有通知的简单提醒应用程序,正如标题所说,我的 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 中,我正在使用以下方法创建一个频道:

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)

       // }

    }

最后我在一个包含新提醒表单的片段中创建通知。在创建表单(提交按钮的 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()

通知从未显示,而且我几天来一直对我错过了什么一无所知。在此先感谢您的帮助:)

【问题讨论】:

  • 只添加你的通知通道就可以了,记得在启动应用程序之前创建

标签: java android android-studio kotlin android-notifications


【解决方案1】:

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

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

解决方案

步骤 1. 创建通知渠道-

     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. 在您的应用程序类中注册通道

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">

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

【讨论】:

    【解决方案2】:

    只添加你的通知频道,记得在你启动应用程序时创建

    【讨论】:

      【解决方案3】:

      通知级别是在首次安装应用时设置的,如果级别发生变化,您需要卸载应用并重新安装。我已经被这个抓了好几次了。

      【讨论】:

        猜你喜欢
        • 2012-01-05
        • 1970-01-01
        • 1970-01-01
        • 2013-12-01
        • 2023-03-04
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 2016-04-17
        相关资源
        最近更新 更多