Reputation: 245
Running this example in the Android Studio debugger in order to learn how notifications work. Version 33 of API.
@Override
public void onClick(View v) {
// Show the notification
Log.d("click", "clicked");
// Create a notification channel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"channel_id",
"Channel Name",
NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
// Build the notification
Notification.Builder builder = new Notification.Builder(this, "channel_id")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Notification Title")
.setContentText("Notification Text");
notificationManager.notify(1, builder.build());
}
Any idea why the notificationManager
reference becomes null after calling notificationManager.createNotification(channel)
? Do I need to still use NotificationCompat
even if I'm only interested in running my code right now at API level 33?
Upvotes: 0
Views: 758