No notifications in Oreo

I’m having trouble seeing any notifications with a targetSdkVersion 26. When I get a new result there is a log statement, but no notification.

I’ve added a channel to the NotificationManager, but that doesn’t seem to do anything.

The only other log statements are:
W/Notification: Use of stream types is deprecated for operations other than volume control
W/Notification: See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case

My code in onHandleIntent(Intent) in PollService.java:

        ...
        Log.i(TAG, "Got a new result " + resultId);
		
        String CHANNEL_ID = "channel_1";
		Resources resources = getResources();
		Intent i = PhotoGalleryActivity.newIntent(this);
		PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);

		NotificationManager mNotificationManager =
				(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

		NotificationChannel mChannel;
		int importance = NotificationManager.IMPORTANCE_DEFAULT;

		NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
				.setTicker(resources.getString(R.string.new_pictures_title))
				.setSmallIcon(android.R.drawable.ic_menu_report_image)
				.setContentTitle(resources.getString(R.string.new_pictures_title))
				.setContentText(resources.getString(R.string.new_pictures_text))
				.setContentIntent(pi);

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
			mChannel = new NotificationChannel(CHANNEL_ID, this.getString(R.string.app_name), importance);
			// Configure the notification channel.
			mChannel.setDescription("notification");
			mNotificationManager.createNotificationChannel(mChannel);
		} else {
			builder.setContentTitle(resources.getString(R.string.new_pictures_title))
					.setPriority(NotificationCompat.PRIORITY_DEFAULT)
					.setAutoCancel(true);
		}

		builder.setChannel(CHANNEL_ID);
		mNotificationManager.notify(0, builder.build());
2 Likes

Solved!

The constructor for NotificationCompat.Builder(Context) was deprecated.
Had to use the constructor NotificationCompat.Builder(Context, String)

1 Like

thanks for sending answer.

Guys, if anyone has troubles with Oreo notifications in background (when app is not opened or device is blocked), there is Evernote library I used in 28-29 Chapters to do the same thing as in the book. Here is the solution: https://github.com/bekabot/PhotoGallery/tree/Chapter-29-with-Evernote

I am having the same problem, no notifications come through, I even added a second one with different text and title values to see if a Notification would appear, and nothing. I am getting the log messages though. Can anyone help?

My code worked for this:

Log.i(TAG, "Got a new result: " + resultId);

Log.i(TAG, “-----Notification start”);

String CHANNEL_ID = “channel_1”;
Resources resources = getResources();
Intent i = PhotoGalleryActivity.newIntent(this);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);

NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Notification.Builder builder = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder = new Notification
.Builder(this, CHANNEL_ID)
.setSmallIcon(android.R.drawable.ic_menu_report_image)
.setContentTitle(resources.getString(R.string.new_pictures_title))
.setContentText(resources.getString(R.string.new_pictures_text))
.setContentIntent(pi)
.setAutoCancel(true);
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
getString(R.string.new_pictures_title), NotificationManager.IMPORTANCE_DEFAULT);
assert mNotificationManager != null;
mNotificationManager.createNotificationChannel(channel);

} else {
builder = new Notification
.Builder(this)
.setSmallIcon(android.R.drawable.ic_menu_report_image)
.setContentTitle(resources.getString(R.string.new_pictures_title))
.setContentText(resources.getString(R.string.new_pictures_text))
.setContentIntent(pi)
.setAutoCancel(true);
}

mNotificationManager.notify(0, builder.build());

Log.i(TAG, “-----Notification finished”);