有条件地显示前台服务通知查看

如何解决有条件地显示前台服务通知查看

我正在开发一个类似聊天的应用程序模块,它需要频繁轮询(每 3 秒一次)以从服务器获取新消息。目前我正在通过使用前台服务来管理一个单独的线程来进行轮询。

由于这是一项前台服务,因此需要通知,我通过创建低优先级/重要性来设置通知,以便通知仅在状态栏中显示为图标。

protected void createNotificationChannel() {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,title,NotificationManager.IMPORTANCE_DEFAULT);

        channel.setDescription(title);
        channel.setShowBadge(true);
        channel.enableVibration(true);
        channel.setSound(null,null);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if(notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }
}

// showNotification is true if the Notification "peek" should be displayed,and
// false to just display the notification in the status bar
public NotificationCompat.Builder buildNotification(boolean showNotification) {
    List<Message> messageList = getMessageList();
    Message oldest = messageList.size() > 0 ? messageList.get(0) : null;
    Message newest = messageList.size() > 0 ? messageList.get(messageList.size() - 1) : null;

    // Create the notification
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext(),channelId)
            // Set notification data and appearance
            .setSmallIcon(smallIcon)
            .setColor(ContextCompat.getColor(getContext(),R.color.moxie_primary_color))

            // Set notification options
            .setPriority(showNotification ? NotificationCompat.PRIORITY_HIGH : NotificationCompat.PRIORITY_MIN)
            .setDefaults(showNotification ? NotificationCompat.DEFAULT_VIBRATE : 0)
            .setDefaults(0)
            .setOnlyAlertOnce(false);

    // If this is a stacking notification,set up the appropriate notification style
    NotificationCompat.MessagingStyle style = new NotificationCompat.MessagingStyle(new Person.Builder().setName("Me").build());
    for (Message message : getMessages()) {
        style.addMessage(
                message.message,message.when,new Person.Builder().setName(message.sender).build()
        );
    }
    builder.setStyle(style);

    return builder;
}

当 Activity 处于前台并且通知在状态栏中无声可见且不显示窥视时,这可以正常工作,这很合适,因为消息实际上是在聊天 Activity 本身中实时显示的。

当我将活动移到后台时出现问题。那时我希望向用户显示通知查看,以便他们可以看到(并响应)传入的消息。到目前为止,我还没有找到真正实现这一目标的方法。在 showNotification 设置为 true 的情况下重新发布由上述代码创建的通知,这会更改优先级和默认值似乎并没有实际工作。

有什么方法可以使用前台服务通知并有条件地显示或隐藏通知查看?

对于频繁的后台轮询,是否有比前台服务更好的选择(我之前根据操作系统使用过 TimerJobService,但由于 Android 10 导致轮询延迟过长而放弃了使用)和复杂的过程)?这样我只能在活动在后台发布通知并且它的优先级/重要性永远不需要改变。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com(将#修改为@)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?