I am trying to display notification when we enable button show notification button from my app and hide when i click on hide notification from my app , also if i will try to dismiss notification from notification section, notification should not dismiss notification should dismiss only when i click on stop notification from my app hide button section . for this i have created service like below code.
public class MyService extends Service {
private String TAG = "MyService";
public static boolean isServiceRunning;
private String CHANNEL_ID = "NOTIFICATION_CHANNEL";
AppPreference appPreference;
NotificationManager notificationManager;
public MyService() {
Log.d(TAG, "constructor called");
isServiceRunning = false;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate called");
appPreference = new AppPreferenceImpl(getBaseContext());
isServiceRunning = true;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand called");
Intent notificationIntent = new Intent(this, AppLockActivity.class);
notificationIntent.putExtra( "NotificationMessage" , "I am from Notification" ) ;
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, getString(R.string.notification_channel_id))
.setContentTitle(appPreference.getNotificationMsg().getMainText())
.setContentText(appPreference.getNotificationMsg().getSubText())
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent)
.setContentInfo(getString(R.string.app_name))
.setColor(Color.parseColor("#80512D6D"))
.setLights(Color.RED, 1000, 300)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setSmallIcon(R.drawable.ic_notification);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
createNotificationChannel();
assert notificationManager != null;
startForeground(1, notificationBuilder.build());
return START_STICKY;
}
private void createNotificationChannel() {
appPreference.setStickyNotification(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String appName = getString(R.string.app_name);
NotificationChannel serviceChannel = new NotificationChannel(
getString(R.string.notification_channel_id),appName, NotificationManager.IMPORTANCE_DEFAULT
);
notificationManager.createNotificationChannel(serviceChannel);
}
}
@Override
public void onDestroy() {
appPreference.setStickyNotification(false);
Log.d(TAG, "onDestroy called");
isServiceRunning = false;
stopForeground(true);
super.onDestroy();
}
}
start service when we click on show notification using below code
fun startService() {
Log.d("TAG", "startService called")
if (!MyService.isServiceRunning) {
val serviceIntent = Intent(this, MyService::class.java)
ContextCompat.startForegroundService(this, serviceIntent)
}
}
hide notification when we click hide notification button and we stopping service
fun stopService() {
Log.d("TAG", "stopService called")
if (MyService.isServiceRunning) {
val serviceIntent = Intent(this, MyService::class.java)
stopService(serviceIntent)
}
}
this working on all device except it's show notification and hide notification button click but its also dismiss when i try to dismiss from notification i don't want dismiss notification from notification section when i click on clear all notification it should dismiss only when we click hide or show from app this problem is coming could any one help me in this .