Manifest.permission.POST_NOTIFICATIONSが必要になりました。
private fun showNotification() {
// 通知をタップしたら、SubActivityを表示するためのintent
val intent = Intent(this, SubActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
)
val builder = NotificationCompat.Builder(this, "channelId")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("タイトル")
.setContentText("テキスト")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
val nm = NotificationManagerCompat.from(this)
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.POST_NOTIFICATIONS
) != PackageManager.PERMISSION_GRANTED
) {
return
}
nm.notify(0, builder.build())