john
john

Reputation: 9

Android cancel ongoing notification not working

There seems to be other questions regarding cancelling ongoing notification.
However, i really look into quite a number of them and still have no solution.

SettingFragment
public class SettingFragment extends Fragment {
    private Switch mSwitchNotific;
    mSwitchNotific.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                                     boolean isChecked) {
            if(isChecked){
                Log.v(LOGTAG, "step notification on");
                getContext().startService(new Intent(getContext(), UpdateStepNotificationService.class));
            }else{
                Log.v(LOGTAG, "step notification off");
                NotificationManager notificationManager = (NotificationManager)
                        getContext().getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.cancel(StickyUpdateStepNotification.NOTIFICATION_ID);
            }
        }
    }
}


UpdateStepNotificationService
    public class UpdateStepNotificationService extends Service {
        static NotificationManager notificationManger;
        StepsDBHandler stepsDBHandler;

        public UpdateStepNotificationService() {}

        @Override
        public void onCreate() {
            super.onCreate();
            stepsDBHandler = new StepsDBHandler(getApplicationContext(), null, null, 0);
            notificationManger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            PendingIntent pendingIntent = PendingIntent.getActivity(this,
                StickyUpdateStepNotification.NOTIFICATION_ID,
                new Intent(getApplicationContext(), MainActivity.class),
                PendingIntent.FLAG_UPDATE_CURRENT);

            final Notification notification = new Notification.Builder(
            getApplicationContext())
                .setSmallIcon(R.drawable.ic_notice)
                .setContentTitle("Walksapp")
                .setContentText("Now: "    +Integer.toString(stepsDBHandler.getCurrentStepToday()) + " steps")
            .setContentIntent(pendingIntent)
            .build();
            notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                notificationManger.notify(StickyUpdateStepNotification.NOTIFICATION_ID, notification);
                }
            }, 0, 1000);
        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            super.onStartCommand(intent, flags, startId);
            return START_STICKY;
        }

        @Override
        public void onDestroy() {
        notificationManger.cancel(StickyUpdateStepNotification.NOTIFICATION_ID);
        super.onDestroy();
        }
}


UpdateStepNotificationService for repeating issue new notification with latest step count.
The notification is with FLAG_NO_CLEAR and FLAG_ONGOING_EVENT.
The only way turn on and off the notification is by switch button in SettingFragment.

Attempt:
notificationManger.cancel(StickyUpdateStepNotification.NOTIFICATION_ID);
Not working

Hope to see any input
Thanks

Upvotes: 0

Views: 845

Answers (1)

Dan Harms
Dan Harms

Reputation: 4840

You will need to kill the timer otherwise it will continue updating the notification.

Upvotes: 1

Related Questions