NotificationCompat with API 26

Create the NotificationChannel only if API >= 26 public void initChannels(Context context) { if (Build.VERSION.SDK_INT < 26) { return; } NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationChannel channel = new NotificationChannel(“default”, “Channel name”, NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription(“Channel description”); notificationManager.createNotificationChannel(channel); } And then just use: NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, “default”); So your notifications are working with both API … Read more

Cancel notification on remove application from multitask panel

Killing Notifications when main app has been killed. Since your notification and your app are handled in different threads killing your app via MultitaskManager won’t kill your notification. As you already correctly investigated killing your app won’t even necesarrily result in an onExit() callback. So what is the solutions? You could start a service from … Read more

How to fix: android.app.RemoteServiceException: Bad notification posted from package *: Couldn’t create icon: StatusBarIcon

What was happening was, I was including the integer reference to the icon in the PendingIntent bundle, and that integer was later being referenced while being posted to the NotificationManager. In between getting the integer reference and the pending intent going off, the app was updated and all of the drawable references changed. The integer … Read more

NotificationCompat.Builder deprecated in Android O

It is mentioned in the documentation that the builder method NotificationCompat.Builder(Context context) has been deprecated. And we have to use the constructor which has the channelId parameter: NotificationCompat.Builder(Context context, String channelId) NotificationCompat.Builder Documentation: This constructor was deprecated in API level 26.0.0-beta1. use NotificationCompat.Builder(Context, String) instead. All posted Notifications must specify a NotificationChannel Id. Notification.Builder Documentation: … Read more

How to create a notification with NotificationCompat.Builder?

The NotificationCompat.Builder is the most easy way to create Notifications on all Android versions. You can even use features that are available with Android 4.1. If your app runs on devices with Android >=4.1 the new features will be used, if run on Android <4.1 the notification will be an simple old notification. To create … Read more

Adding button action in custom notification

Start notification as: private void startNotification(){ String ns = Context.NOTIFICATION_SERVICE; NotificationManager notificationManager = (NotificationManager) getSystemService(ns); Notification notification = new Notification(R.drawable.ic_launcher, null, System.currentTimeMillis()); RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.mynotification); //the intent that is started when the notification is clicked (works) Intent notificationIntent = new Intent(this, FlashLight.class); PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.contentView = notificationView; … Read more