Skip to content Skip to sidebar Skip to footer

How To Remove Notification Instantly In Android

In my application i am showing a notification. From service i am sending the time of login to json. It is checking in server that is there any new data available or not if yes then

Solution 1:

Maybe this answer will help others: In dashboard activity i have changed my time and save it through shared preferences. And in service class i have check that old time and the latest time are same or not. If they are same it will remove notification otherwise it will fire notification through the given condition. here is the code of broadcast notification function:

  private void broadcastNotification(int no_of_review, String service_name) {

    Intent startActivityIntent = new Intent(this, DashboardActivity.class); 
    startActivityIntent.putExtra("NotificationMessage", service_notification);
    startActivityIntent.putExtra("flag", 1);
    startActivityIntent.putExtra("notificationID", NOTIFICATION_ID);
    startActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent launchIntent = PendingIntent.getActivity(this, 0,
            startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);    
    UserFunctions userFunctions = new UserFunctions();
    if (userFunctions.isUserLoggedIn(getApplicationContext())) {    
        /* "formattedDate" is the date saved in shared preferences*/
        if(!formattedDate.equals(service_notification)){
        startActivityIntent.putExtra("latest_time", service_notification);
        Log.d("latest_time", service_notification);
        startActivityIntent.putExtra("flag", 1);
        earthquakeNotificationBuilder
                .setContentIntent(launchIntent)
                .setContentTitle(no_of_review + " " + "new review is there")
                .setAutoCancel(true);
        // .setContentText(service_name);

        if (no_of_review > 10) {
            Uri ringURI = RingtoneManager
                    .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            earthquakeNotificationBuilder.setSound(ringURI);
        }

        double vibrateLength = 100 * Math.exp(0.53 * no_of_review);
        long[] vibrate = new long[] { 100, 100, (long) vibrateLength };
        earthquakeNotificationBuilder.setVibrate(vibrate);

        int color;
        if (no_of_review < 2)
            color = Color.GREEN;
        else if (no_of_review < 5)
            color = Color.YELLOW;
        else
            color = Color.RED;

        earthquakeNotificationBuilder.setLights(color, (int) vibrateLength,
                (int) vibrateLength);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(NOTIFICATION_ID,
                earthquakeNotificationBuilder.getNotification());
    } else {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancelAll();
        }
    }else {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancelAll();
        }
}

Post a Comment for "How To Remove Notification Instantly In Android"