Question Detail

I have Implemented Local Notification in Android Application it is working in all mobile version well except Android Oreo (Android V8.0).

How can I create Notification in Android Application in android Oreo 8.0?

How to implement Notification in Android Oero?


Thread Reply

Hemant Sharma

- 5 months ago

You have to use NotificationChannel Class to implement Notification till you Android Oreo Version.

public void displayNotification(Context activity, int notiCode) {
    String title = activity.getString(R.string.app_name) + " is Active";

    Intent tapIntent = new Intent(activity, DashboardActivity.class);
    notificationManager = (NotificationManager) activity.getSystemService(NOTIFICATION_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getActivity(activity, 1, tapIntent, 0);

    Notification.Builder builder = new Notification.Builder(activity)
            .setContentTitle(title)
            .setSmallIcon(R.drawable.notification_icon)
            .setAutoCancel(false)
            .setSound(null)
            .setContentIntent(pendingIntent).setOngoing(true)
            .setContentText("This is Content!");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(notiCode + "", title, NotificationManager.IMPORTANCE_HIGH);
        builder.setChannelId(notiCode + "");
        mChannel.setSound(null, null);
        notificationManager.createNotificationChannel(mChannel);
    }
    if (Pref.getBoolean(activity, "showcurrentRej_pref")) {
        if (Pref.getInteger(activity, "messageCount_pref") > 0 || Pref.getInteger(activity, "callsCount_pref") > 0 || Pref.getInteger(activity, "notificationCount_pref") > 0) {
            builder.setStyle(new Notification.BigTextStyle(builder)
                    .setBigContentTitle("Rejected Events")
                    .bigText("Total Rejected Calls: " + Pref.getInteger(activity, "callsCount_pref") +
                            "\nTotal Rejected SMS : " + Pref.getInteger(activity, "messageCount_pref") +
                            "\nTotal Rejected Notification : " + Pref.getInteger(activity, "notificationCount_pref"))
                    .setSummaryText("More")).setContentTitle(title);
        }
    }
    Notification notification = builder.build();
    notificationManager.notify(notiCode, notification);
}

Hemant Sharma

- 5 months ago

You have to use NotificationChannel Class to implement Notification till you Android Oreo Version.

public void displayNotification(Context activity, int notiCode) {
    String title = activity.getString(R.string.app_name) + " is Active";

    Intent tapIntent = new Intent(activity, DashboardActivity.class);
    notificationManager = (NotificationManager) activity.getSystemService(NOTIFICATION_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getActivity(activity, 1, tapIntent, 0);

    Notification.Builder builder = new Notification.Builder(activity)
            .setContentTitle(title)
            .setSmallIcon(R.drawable.notification_icon)
            .setAutoCancel(false)
            .setSound(null)
            .setContentIntent(pendingIntent).setOngoing(true)
            .setContentText("This is Content!");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(notiCode + "", title, NotificationManager.IMPORTANCE_HIGH);
        builder.setChannelId(notiCode + "");
        mChannel.setSound(null, null);
        notificationManager.createNotificationChannel(mChannel);
    }
    if (Pref.getBoolean(activity, "showcurrentRej_pref")) {
        if (Pref.getInteger(activity, "messageCount_pref") > 0 || Pref.getInteger(activity, "callsCount_pref") > 0 || Pref.getInteger(activity, "notificationCount_pref") > 0) {
            builder.setStyle(new Notification.BigTextStyle(builder)
                    .setBigContentTitle("Rejected Events")
                    .bigText("Total Rejected Calls: " + Pref.getInteger(activity, "callsCount_pref") +
                            "\nTotal Rejected SMS : " + Pref.getInteger(activity, "messageCount_pref") +
                            "\nTotal Rejected Notification : " + Pref.getInteger(activity, "notificationCount_pref"))
                    .setSummaryText("More")).setContentTitle(title);
        }
    }
    Notification notification = builder.build();
    notificationManager.notify(notiCode, notification);
}