Fragment Cancel Local Notification – Android-ThrowExceptions

Exception or error:

I am sending notifications in Local. There is nothing wrong with the notification, the notification is coming. But I can’t turn off the notification. Although I canceled the notification in fragment, the notification still comes. What do I need to cancel my notification?

public class suBildirimFragment extends Fragment implements AdapterView.OnItemSelectedListener {

if ...{
     AlarmManager aManager = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
                           Intent intent = new Intent(getActivity(), AlarmReceiver.class);
                           PendingIntent pIntent = PendingIntent.getBroadcast(getContext(), 0, intent,              PendingIntent.FLAG_UPDATE_CURRENT);
                           aManager.cancel(pIntent);
    }
    }

    public class AlarmReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            NotificationHelper notificationHelper = new NotificationHelper(context);
            NotificationCompat.Builder nb = notificationHelper.getChannelNotification();
            notificationHelper.getManager().notify(1, nb.build());
        }
    }

    public class NotificationHelper extends ContextWrapper {
        public static final String channelID = "suBildirim";
        public static final String channelName = "Bildirim";

        private NotificationManager mManager;

        public NotificationHelper(Context base) {
            super(base);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                createChannel();
            }
        }

        @TargetApi(Build.VERSION_CODES.O)
        private void createChannel() {
            NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_DEFAULT);

            getManager().createNotificationChannel(channel);
        }

        public NotificationManager getManager() {
            if (mManager == null) {
                mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            }

            return mManager;
        }

        public NotificationCompat.Builder getChannelNotification() {
            return new NotificationCompat.Builder(getApplicationContext(), channelID)
                    .setContentTitle("...!")
                    .setContentText("...!!.")
                    .setSmallIcon(R.mipmap...);
        }
    }
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *