I want to make custom notification in my application, I found an example of what I need on en-So . I did everything as in the example, but the notification does not pop up, although it is displayed in the dock.

How can I make the notification pop up? Or maybe there are some other alternative ways?

Here is the code I use:

 RemoteViews remoteViews = new RemoteViews(activity.getPackageName(), R.layout.notification_widget); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( activity).setContentTitle("sdgasgsag").setSmallIcon(R.drawable.icon).setContent( remoteViews); // Creates an explicit intent for an Activity in your app Intent resultIntent = new Intent(activity, activity.getClass()); // The stack builder object will contain an artificial back stack for // the // started Activity. // This ensures that navigating backward from the Activity leads out of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(activity); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(activity.getClass()); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.button1, resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE); // mId allows you to update the notification later on. mNotificationManager.notify(100, mBuilder.build()); 
  • This is not clear notification не всплывает хотя в доке он отображается. - Yuriy SPb
  • @YuriySPb well, in the sense that if you lower the curtain, then this notification is visible in it, but it doesn’t come up with the curtain closed. I do not know how to explain it differently. Those. I only see a small icon in the stat bar, but I don’t see a pop-up alert. - Kirill Stoianov
  • Do you have it on all axes and devices? - Yuriy SPb
  • I tested on Nexus 4 with firmware 5.2, I will try on other devices! Could it be in the device itself? - Kirill Stoianov
  • Anything can be. But I would put the device / axis settings on the reaction to notifications in different modes (blocked / not) and on the priority / type of notifications - YuriySPb

1 answer 1

In short, like this:

  Intent i = new Intent(context, MainScreen.class); PendingIntent pi = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), i, PendingIntent.FLAG_CANCEL_CURRENT); android.app.Notification.Builder builder = new android.app.Notification.Builder(context) .setContentTitle(title) .setContentText(text) .setTicker(title) .setSmallIcon(R.drawable.ic_notification) .setContentIntent(pi) .setWhen(System.currentTimeMillis()) .setAutoCancel(true) .setDefaults(android.app.Notification.DEFAULT_ALL); android.app.Notification notification; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { //noinspection deprecation notification = builder.getNotification(); } else { notification = builder.build(); } NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); try { notificationManager.notify(0, notification); } catch (Exception e) { Log.e(TAG, "showAndroidNotification()", e); }