我试图在经过一段时间后发出通知。为了实现这一点,我有一个继承自BroadCast接收器的AlarmReceiver类,适用于运行API 23的设备。它不能在我当前运行API 27的模拟器上工作。任何线索我都会这样做做错了?
class AlarmReceiver : BroadcastReceiver() {
companion object {
val PRIMARY_CHANNEL = "dj"
}
override fun onReceive(context: Context, intent: Intent) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationIntent = Intent(context, NotificationActivity::class.java)
val stackBuilder = TaskStackBuilder.create(context)
stackBuilder.addParentStack(NotificationActivity::class.java)
stackBuilder.addNextIntent(notificationIntent)
val pendingIntent = stackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT)
if (android.os.Build.VERSION_CODES.O <= android.os.Build.VERSION.SDK_INT){
//I create the notification channel on the next line, but it doesn't seem to work
val notificationChannel = NotificationChannel(PRIMARY_CHANNEL,
"DailyJokes", NotificationManager.IMPORTANCE_DEFAULT)
notificationChannel.lightColor = Color.GREEN
notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
notificationManager.createNotificationChannel(notificationChannel)
val notification = Notification.Builder(context, PRIMARY_CHANNEL)
.setContentIntent(pendingIntent)
.setChannelId("dj")
.setContentText("KDW")
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle("CCC")
.build()
notificationManager.notify(1,notification)
} else {
var builder = NotificationCompat.Builder(context, "dj")
val sound = Uri.parse("android.resource://" + context.packageName + "/" + "raw/drumroll")
builder = builder
.setSmallIcon(R.mipmap.ic_launcher_round)
.setColor(Color.BLUE)
.setContentTitle("Content Title")
.setTicker("TICKER Text")
.setContentText("KDW setContentText")
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setChannelId("dj")
.setSound(sound)
notificationManager.notify(1, builder!!.build())
}
}
}
这里是触发通知的代码,这里有什么?
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_notification)
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val notificationIntent = Intent("android.media.action.DISPLAY_NOTIFICATION")
notificationIntent.addCategory("android.intent.category.DEFAULT")
val broadcast = getBroadcast(this, 100, notificationIntent, FLAG_UPDATE_CURRENT)
val cal = Calendar.getInstance()
cal.add(Calendar.SECOND, 5)
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.timeInMillis, broadcast)
}
}
我的Manifest中也有一个接收器:
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.media.action.DISPLAY_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
即使有了这一切,这个bug仍然潜伏着。我是Kotlin的新手,今天我开始做了一个星期。我的快速经历为我做好了准备,但我似乎无法抓住通知:/
答案 0 :(得分:0)
从我的代码中可以看出,您需要在Manifest或上下文中注册接收器。
https://developer.android.com/guide/components/broadcasts.html
答案 1 :(得分:0)
您的频道是否已创建? 你的比较是在错误的方向 - &gt;
android.os.Build.VERSION_CODES.O <= android.os.Build.VERSION.SDK_INT
应该>=
而不是
并且只有通道创建需要防范O,而不是通知创建。