【问题标题】:how to turn off notification programmatically in android when app running?应用程序运行时如何在android中以编程方式关闭通知?
【发布时间】:2016-12-27 10:38:50
【问题描述】:

我正在尝试开发一个 android 应用程序,并在其中实现聊天功能。 但是当我收到消息时,它会发出通知声。 当用户以编程方式使用应用程序时,如何停止通知?

 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());
        }

【问题讨论】:

  • 在发布问题之前,请在互联网上进行一些研究或上一个关于 SO 的问题。

标签: android android-studio push-notification google-cloud-messaging chat


【解决方案1】:
 //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;
                }

【讨论】:

    【解决方案2】:

    第一部分是

    “消息它会发出通知声音。”

    首先检查应用程序是否在前台,如果是的话,要停止声音使用set defaults to 0 and sound to null。像这样更改方法参数.setSound(isInForeGround ? null : uri).

    当用户使用应用程序时如何停止通知 以编程方式?

    我猜这是你应该遵循的;

    就用户体验而言,您不应停止通知, 您只需要以用户不应该看到的方式使用它们 当应用程序在通知栏中打开时,它们会堆积起来。

    就聊天应用程序而言,您应该使用一些东西作为 如果两个用户在线并聊天,则发送接收消息的通道 所以当你收到消息时。

    所以不要从 您在通知栏中收到的特定聊天数据。

    希望这可以清除您的概念消费通知。希望这会有所帮助。

    【讨论】:

    • 我如何更改默认值?
    • @deepak-mittal 只需检查您的应用程序是否在前台,然后在此方法中使用条件运算符 .setSound(isinForeground ? null : uri)。这将解决您的问题。对于前台应用,我敦促您进行一些互联网研究以获得最佳方法......希望这会有所帮助
    • @DEEPAKMITTAL 如果它解决了您的问题,请接受答案。
    【解决方案3】:

    只需删除

    .setSound(defaultSoundUri1) 
    

    来自

     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();
    

    当您不需要任何声音时。 如果这对您没有帮助,请尝试这样做:

    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;
    

    【讨论】:

    • 如果我要删除这个 .setSound(defaultSoundUri1) 。通知永久停止工作。
    【解决方案4】:

    第一组

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

    在 AndroidManifest 中 然后在需要的地方执行此操作

      final AudioManager mode = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
            mode.setRingerMode(AudioManager.RINGER_MODE_SILENT);
    

    【讨论】:

    • 这不会影响所有应用程序,而不仅仅是调用应用程序吗?
    猜你喜欢
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 2017-03-31
    相关资源
    最近更新 更多