2

Hello I wrote a class to create notifications, but the problem that there is the notification icon that appears in the status bar, the notification block does not appear or even in the lock screen. In the doc it is written to use NotificationManagerCompat to show notification ( Docs link). Here is my code:

public class NotificationsService {
    private Context mContext;
    private NotificationManager mNotificationManager;

    public NotificationsService(Context context) {
        mContext = context;
    }

    public void sendNotification(int iconResource) {
        Notification notification = buildNotification(iconResource);
//        mNotificationManager.notify(0 /* ID of notification */, notification);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mContext);
        notificationManager.notify(70, notification);
    }

    public Notification buildNotification(int iconResource) {
        String CHANNEL_ID = "type"; // The id of the channel.
        CharSequence channelName = "dsfsf";

        NotificationChannel channel = null;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            channel = new NotificationChannel(CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_DEFAULT);
        }

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext, CHANNEL_ID)
                .setSmallIcon(iconResource)
                .setContentTitle("title")
                .setContentText("message")
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setSound(defaultSoundUri)
                .setChannelId(CHANNEL_ID);
//                .setContentIntent(pendingIntent);

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && mNotificationManager != null) {
            mNotificationManager.createNotificationChannel(channel);
        }

        return notificationBuilder.build();
    }

And using like this:

NotificationsService notif = new NotificationsService(context);
  notif.sendNotification(R.mipmap.ic_launcher);

Only the app icon appears and the notification only in the status bar

enter image description here

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Mickael Belhassen
  • 2,970
  • 1
  • 25
  • 46

1 Answers1

0

You need to notify the notification manager about your notification. I have developed a method for generating notification for Oreo or upper SDK.

Call this method with the right arguments in your code...

//This method is for Oreo
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static void createSimpleNotificationForOreo(Context context, PendingIntent pendingIntent, String notificationTitle, String notificationContent, int notificationId, String notificationChannelId) {
        Notification.BigTextStyle bigText = new Notification.BigTextStyle();
        bigText.bigText(notificationContent);
        bigText.setBigContentTitle(notificationTitle);
        Notification.Builder notificationBuilder = new Notification.Builder(context)
                .setSmallIcon(R.drawable.ic_small_notification)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                .setColor(context.getResources().getColor(R.color.colorPrimary))
                .setContentText(notificationContent)
                .setStyle(bigText)
                .setChannelId(notificationChannelId)
                .setContentIntent(pendingIntent);

        Notification notification = notificationBuilder.build();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel mChannel = new NotificationChannel(notificationChannelId, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(mChannel);
            notificationManager.notify(notificationId, notification);
        }
    }
Sayan Mukherjee
  • 352
  • 2
  • 21
  • Not working only show the icon on status bar i do testing with Android 8.0.0. You do not use `NotificationManagerCompat` as written in the doc https://developer.android.com/training/notify-user/build-notification#notify – Mickael Belhassen Mar 25 '19 at 10:01
  • Using `NotificationManagerCompat` is not mandatory if your device is running on Android Oreo. The above method is using `@RequiresApi(api = Build.VERSION_CODES.O)` annotation, thus only `NotificationManager` is enough. If you want a backward SDK compatible, then only you should use `NotificationManagerCompat`. The above method is **Tested OK** in the production environment, so there must be some other issues with your device or code. Could you try using any other device? – Sayan Mukherjee Mar 25 '19 at 12:13
  • I found this answer i think its related https://stackoverflow.com/a/42754344/10512612 .. But in the answer there is no official source, do you confirm what he says? Should I overwork that I redirect the user to the screen setting notifications? – Mickael Belhassen Mar 25 '19 at 12:27
  • If the device has a settings option to enable/disable floating notification, it means the device gives the user power of controlling how a notification will show. So the developer should not force it to enable the option as it is not recommended. – Sayan Mukherjee Mar 26 '19 at 07:07