how to turn off notification programmatically in android when app running?
Asked Answered
T

4

3

I am trying to develop an android app in which i am implementing chat functionality. but when i receive the message it makes a notification sound. How can i stop the notification when the user is using the app programmatically?

 private void ChatNotifications(String uid, String fuid, String message) {
            NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle();
            notiStyle.setBigContentTitle("message");
            // notiStyle.setSummaryText(quote);
            notiStyle.bigText(message);
            Intent resultIntent = new Intent(this, Loading.class);

            resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(Loading.class);
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
            //Uri defaultSoundUri1 = Uri.parse("android.resource://com.therevew.quotes/" + R.raw.hello);
            Uri defaultSoundUri1= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Notification myNotification = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
                    //  .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ii_launcher))
                    .setSound(defaultSoundUri1)
                    .setAutoCancel(true)
                    .setColor(getResources().getColor(R.color.notificationColor))
                    .setContentIntent(resultPendingIntent)
                    .setContentTitle("message")
                    .setContentText(message)
                    .setStyle(notiStyle).build();
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(101/* ID of notification */, notiStyle.build());
        }
Tolyl answered 27/12, 2016 at 10:38 Comment(1)
Please do some research over internet or prev questions on SO before posting questions.Moan
M
3

First part is

"message it makes a notification sound."

First check if application is in foreground or not if it is then, To stop sound use set defaults to 0 and sound to null. change the method params like this .setSound(isInForeGround ? null : uri).

How can i stop the notification when the user is using the app programmatically?

I guess this what you should follow;

As per user experience is concerned you should not stop notifications, you just have to consume them in a way user should not able to see them pilled up when app is open in notificationBar.

As Chatting application is concerned you should've used something as channel to send receive messages if two users are online and chatting so when you receive message.

So just don't create pending intent from the data you receive for particular chat in noticiationBar.

Hope this clears your concept consuming notification. Hope this helps.

Moan answered 27/12, 2016 at 10:47 Comment(3)
how i am change default values?Tolyl
@deepak-mittal Just check if your application is in foreground then use Conditional operator in this method .setSound( isinForeground ? null : uri). This will solve your problem. For application in foreground I urge you to do some internet research to get the best approach... hope this helpsMoan
@DEEPAKMITTAL please accept the answer if it solves your problem.Moan
T
3
 //if you use fcm then try       

   @Override
    public void onMessageReceived(final RemoteMessage remoteMessage) {
        if (remoteMessage.getNotification() != null) {
            String message = remoteMessage.getNotification().getBody();
            if (isForeground(getApplicationContext())) {
              //if in forground then your operation
            // if app is running them
            } else {
             //if in background then perform notification operation
                sendNotification(message);
            }
        }
    }


        private static boolean isForeground(Context context) {
                    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
                    List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
                    final String packageName = context.getPackageName();
                    for (ActivityManager.RunningAppProcessInfo appProcess : tasks) {
                        if (ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND == appProcess.importance && packageName.equals(appProcess.processName)) {
                            return true;
                        }
                    }
                    return false;
                }
Torre answered 27/12, 2016 at 10:48 Comment(0)
H
2

Just remove

.setSound(defaultSoundUri1) 

from

 Notification myNotification = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
                    //  .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ii_launcher))
                    .setSound(defaultSoundUri1)
                    .setAutoCancel(true)
                    .setColor(getResources().getColor(R.color.notificationColor))
                    .setContentIntent(resultPendingIntent)
                    .setContentTitle("message")
                    .setContentText(message)
                    .setStyle(notiStyle).build();

when you do not need any sound. If this will not help you, try to to do this:

Notification myNotification = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
                    //  .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ii_launcher))
                    .setSound(null)
                    .setAutoCancel(true)
                    .setColor(getResources().getColor(R.color.notificationColor))
                    .setContentIntent(resultPendingIntent)
                    .setContentTitle("message")
                    .setContentText(message)
                    .setStyle(notiStyle).build();
myNotification.defaults = 0;
Heloise answered 27/12, 2016 at 10:42 Comment(1)
if i am removing this .setSound(defaultSoundUri1) . notification stop work permanently.Tolyl
W
-1

First set

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

in AndroidManifest then do this where required

  final AudioManager mode = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
        mode.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Whirlabout answered 4/10, 2017 at 8:50 Comment(1)
Will this not affect all apps and not just the calling app?Hallucination

© 2022 - 2024 — McMap. All rights reserved.