android - 应用内通知

标签 android notifications

当应用程序启动时,它会从数据库中检索数据。如果有新数据,我希望应用程序显示应用内通知(横幅)以警告用户。

目前我实现了这个:

if (!taskToNotify.isEmpty()) {
            //Notification builder
            NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
            builder.setAutoCancel(true);
            builder.setSmallIcon(R.drawable.ic_event_notification);
            builder.setContentTitle(taskToNotify.get(0).getName() + mContext.getString(R.string.notification_content_task));
            builder.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND);

            // inbox style
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            inboxStyle.addLine(mContext.getString(R.string.notification_detail_date) + taskToNotify.get(0).dueDateToString());
            inboxStyle.addLine(mContext.getString(R.string.notification_detail_location) + taskToNotify.get(0).getLocationName());

            // Moves the expanded layout object into the notification object.
            builder.setStyle(inboxStyle);


            mAdapter.notifyDataSetChanged();

            builder.build();
        }

但这暂时不起作用。代码运行时不显示任何通知。

是否有可能在 Android 手机上执行此操作?或者我应该以另一种方式向用户显示此信息?

感谢您宝贵的帮助:)

最佳答案

你缺少setContentText()否则不满足要求。

When creating a notification, specify the UI content and actions with a NotificationCompat.Builder object. At bare minimum, a Builder object must include the following:

  • 一个小图标,由 setSmallIcon() 设置
  • 标题,由 setContentTitle() 设置
  • 详细文本,由setContentText()设置

https://developer.android.com/training/notify-user/build-notification.html

关于android - 应用内通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41041657/

相关文章:

java - 安卓。替换字符串中的 * 字符

android - 按升序排列 SMS 消息

android - 如何在 Android 上的方向更改时避免重新启动 Activity

java - Facebook 之类的通知或 ajax 调用?

java - 通知不显示所有操作按钮

android - 什么是通知 channel ID?通知在 api 27 中不起作用

java - 如何将 Java 日期格式的日期传递给 cURL (PHP)?

android - 应用内购买 : Two devices with same account not getting same purchased items

android - 在 RemoteView 上调用 setImageViewBitmap 会导致令人难以置信的延迟

notifications - 配置 MantisBT 以使用 GMail 发送电子邮件