Question Detail

I need a notification when my Android Start Working and the Notification can not clear by the user.

Notification is stayed there eighter App stop working. I have searched on google for 

un-clearable Notification by the user in android Programmatically?


Thread Reply

Neeraj Mehra

- 5 months ago

public static final int NOTIFICATION_ID = 1; //this can be any int


//Building the Notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_stat_notification);
builder.setContentTitle("BasicNotifications Sample");
builder.setContentText("Time to learn about notifications!");

NotificationManager notificationManager = (NotificationManager) getSystemService(
            NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());

- 5 months ago

showNotification(getString(R.string.app_name) + " is Active"); //Call this function from where you want to Show None clearable Notification 
clearNotification(); // Call this function when your Android Application Stop working and want to clear None Clearable notification

 

NotificationManager notificationManager;
  public void showNotification(String title) {
// Use NotificationCompat.Builder to set up our notification.
        NotificationCompat.Builder ncBuilder = new NotificationCompat.Builder(this);
//icon appears in device notification bar and right hand corner of notification
        ncBuilder.setSmallIcon(R.drawable.ic_app_icon);
// This intent is fired when notification is clicked
        Intent tapIntent = new Intent(this, DashboardActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, tapIntent, 0);
// Set the intent that will fire when the user taps the notification.
        ncBuilder.setContentIntent(pendingIntent);
        ncBuilder.setOngoing(true); // Cant cancel your notification (except NotificationManger.cancel(); )
// Content title, which appears in large type at the top of the notification
        ncBuilder.setContentTitle(title);
// Content text, which appears in smaller text below the title
        ncBuilder.setContentText("We will take care of Mobile events! Focus on your Driving!");
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Will display the notification in the notification bar
        notificationManager.notify(6546, ncBuilder.build());
    }
    public void clearNotification() {
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(6546); // Notification ID to cancel
    }

Hemant Sharma

- 5 months ago

NotificationManager manager;
Notification myNotication;
 public void displayNotification(String title) {
        Intent tapIntent = new Intent(this, DashboardActivity.class);
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getActivity(DashboardActivity.this, 1, tapIntent, 0);
        Notification.Builder builder = new Notification.Builder(DashboardActivity.this);
        builder.setAutoCancel(false);
        builder.setTicker(title);
        builder.setContentTitle(title);
        builder.setContentText("Focus on Driving,We'll handle mobile Events!");
        if (Pref.getInteger(mActivity, "messageCount_pref") > 0 || Pref.getInteger(mActivity, "callsCount_pref") > 0 || Pref.getInteger(mActivity, "notificationCount_pref") > 0) {
            builder.setStyle(new Notification.BigTextStyle(builder)
                    .setBigContentTitle("Rejected Events")
                    .bigText("TOTAL REJECTED CALLS: " + Pref.getInteger(mActivity, "callsCount_pref") + " \nTOTAL REJECTED SMS : " + Pref.getInteger(mActivity, "messageCount_pref") + " \nTOTAL REJECTED NOTIFICATION : " + Pref.getInteger(mActivity, "notificationCount_pref"))
                    .setSummaryText("More")).setContentTitle(title);
        }
        builder.setSmallIcon(R.drawable.ic_speed_meter);
        builder.setContentIntent(pendingIntent);
        builder.setOngoing(true);
//        builder.setNumber(100);
        builder.build();
        myNotication = builder.getNotification();
        manager.notify(11, myNotication);
    }

    public void cancelNotification() {
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(11); // Notification ID to cancel
    }

Anonymous

- 5 months ago

In Your Above Lollipop Version unclearable Notification

String title = activity.getString(R.string.app_name) + " is Active";

Intent tapIntent = new Intent(activity, MainActivity.class);
manager = (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.ic_app_icon)
        .setContentIntent(pendingIntent).setOngoing(true)
        .setContentText("Focus on Driving,We'll handle mobile Events!");
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);
}
builder.setAutoCancel(false);
Notification notification = builder.build();
manager.notify(notiCode, notification);