应用程序运行时如何在Android中以编程方式关闭通知? [英] how to turn off notification programmatically in android when app running?

查看:274
本文介绍了应用程序运行时如何在Android中以编程方式关闭通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

第一部分是

消息发出通知声音."

"message it makes a notification sound."

首先检查应用程序是否在前景中,否则,请使用set defaults to 0 and sound to null停止声音.更改方法参数,例如.setSound(isInForeGround ? null : uri).

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;

根据用户体验,您不应该停止通知, 您只需要以用户不应该看到的方式使用它们 在notificationBar中打开应用程序时,它们会堆积起来.

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.

所以不要从中创建挂起的意图 在noticiationBar中收到的用于特定聊天的数据.

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.

这篇关于应用程序运行时如何在Android中以编程方式关闭通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆