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

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

概述我正在尝试开发一个Android应用程序,我正在实现聊天功能.但是当我收到消息时它发出通知声音.当用户以编程方式使用应用程序时,如何停止通知?privatevoidChatNotifications(Stringuid,Stringfuid,Stringmessage){NotificationCompat.BigTextStylenotiStyle

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

总结

以上是内存溢出为你收集整理的当app运行时,如何在android中以编程方式关闭通知?全部内容,希望文章能够帮你解决当app运行时,如何在android中以编程方式关闭通知?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1111258.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存