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

时间:2016-12-27 10:38:51

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

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

4 个答案:

答案 0 :(得分:3)

第一部分是

  

“它会发出通知声。”

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

  

如何在用户使用该应用时停止通知   编程?

我想你应该遵循这个;

  

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

     

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

     

所以只是不要创建待定意图   您在noticiationBar中收到的特定聊天数据。

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

答案 1 :(得分: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;
                }

答案 2 :(得分: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;

答案 3 :(得分:-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);
相关问题