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

问题描述 投票:3回答:4

我正在尝试开发一个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());
        }
android android-studio push-notification google-cloud-messaging chat
4个回答
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;
                }

3
投票

第一部分是

“它会发出通知声。”

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

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

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

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

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

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

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


1
投票

只需删除

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

-1
投票

第一集

<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);
© www.soinside.com 2019 - 2024. All rights reserved.