Issue with Notifications in Oreo
Issue with Notifications in Oreo
Google made a huge breaking change in Oreo - they stopped notifications from working in all apps. I'm now dealing with changing all my apps but I'm left with a couple problems . . .
The notification now gets posted and when the user touches the notification it properly triggers the app. However, (1) the notification does not disappear. To get rid of it the user must delete it. Also (2) the dots on my app icon stay at One after posting several notifications.
I'm using the following notification helper class I found posted by Bipin Pandey:
public class NotificationHelper {
private Context mContext;
private NotificationManager mNotificationManager;
private NotificationCompat.Builder mBuilder;
public static final String NOTIFICATION_CHANNEL_ID = "10001";
public NotificationHelper(Context context) {
mContext = context;
}
/**
* Create and push the notification
*/
public void createNotification(String title, String message)
{
/**Creates an explicit intent for an Activity in your app**/
Intent resultIntent = new Intent(mContext , MainActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
0 /* Request code */, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.drawable.ticon);
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(false)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent);
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
}
}
The following code executes this helper class
NotificationHelper myNotify = new NotificationHelper(context);
myNotify.createNotification("New Message",mesText);
2 Answers
2
You set setAutoCancel(false), so the notification is correctly not automatically being cancelled when you trigger it. Remove that line if you want tapping on the notification to remove the notification (and the dots associated with the notifications).
setAutoCancel(false)
That number is just a count of notifications you see in the notification drawer. From your code, your
notify() call always uses the same id, so each notification just replaces the previous ones rather than creating multiple separate notifications.– ianhanniballake
Jul 2 at 19:17
notify()
You have kept setAutoCancel property as false. So notification won't be dismissed when the user touches it. This is for situations when you want to handle removing the notification at your own end.
Remove that line from your NotificationBuilder and your logic should work as expected.
Description of the Method :
public Notification.Builder setAutoCancel (boolean autoCancel)
Make this notification automatically dismissed when the user touches it.
Read more about Notification Auto Cancel here :
https://developer.android.com/reference/android/app/Notification.Builder.html#setAutoCancel(boolean)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thanks for the quick answer. Do you happen to know why my dot always says "1" when multiple notifications have been created?
– Dean Blakely
Jul 2 at 19:14