当app运行时,如何在android中以编程方式关闭通知?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当app运行时,如何在android中以编程方式关闭通知?相关的知识,希望对你有一定的参考价值。

我正在尝试开发一个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());
        }
答案
 //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;
                }
另一答案

第一部分是

“它会发出通知声。”

首先检查应用程序是否在前台,如果是,则停止声音使用set defaults to 0 and sound to null。像这个.setSound(isInForeGround ? null : uri).改变方法params

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

我想这就是你应该遵循的;

根据用户体验,您不应该停止通知,您只需要以一种方式使用它们,当用户在notificationBar中打开应用程序时,用户不应该看到它们被填满。

作为聊天应用程序,如果两个用户在线聊天,那么当你收到消息时,你应该使用某些东西作为频道来发送接收消息。

因此,不要从noticiationBar中为特定聊天收到的数据创建待定意图。

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

另一答案

只需删除

.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;
另一答案

第一集

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

以上是关于当app运行时,如何在android中以编程方式关闭通知?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 android 中以编程方式在小米手机安全应用程序中为我的应用程序启用自动启动选项

如何在android中以编程方式上传到服务器之前减小视频的大小[关闭]

ios icloud 在设备中以编程方式设置开/关

如何在android中以编程方式授予系统级权限?

在android中以编程方式创建视图时如何传递AttributeSet

如何在android中以编程方式检测MIUI ROM?