【问题标题】:Android notification shown only icon on status barAndroid 通知仅在状态栏上显示图标
【发布时间】:2016-08-10 10:42:04
【问题描述】:

在我的应用程序中,我编写了简单的通知功能,该方法仅在状态栏上显示图标,我也想显示通知布局,但未自动显示,我已将 android 状态栏下拉以查看,

public static void createNotification(Context context, Class activity, String title, String subject, int count_unread_message) {

    Intent intent = new Intent(context, activity);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);

    Notification notify = new NotificationCompat.Builder(context)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentText(subject)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setNumber(count_unread_message)
            .setPriority(0)

            .setLights(Color.BLUE, 500, 500)
            .setContentIntent(pendingIntent)
            .build();

    notify.flags |= Notification.FLAG_AUTO_CANCEL;

    NOTIFICATIONMANAGER = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    NOTIFICATIONMANAGER.notify(159753456, notify);
}

看来这个问题是针对android 6及以上的

【问题讨论】:

    标签: android


    【解决方案1】:

    对我来说,这是通过在构建器上调用 setDefaults(Notification.DEFAULT_ALL) 来解决的。希望对你有帮助,

    保罗

    【讨论】:

    • 在我的情况下,不推荐使用的方法 #setDefaults() 和 #setPriority() 都需要
    【解决方案2】:

    我相信您的问题是没有显示通知横幅,对吧?所以用户必须降低状态栏才能看到通知。

    对我来说,解决的方法是为通知设置更高的优先级。在您的情况下,您应该将其设置为高或最大值(因为它已经为 0 - 默认值):

                .setPriority(1)//1 is high, 2 is max
    

    参考:NotificationCompat.Builder documentation

    【讨论】:

    • 在我的情况下,不推荐使用的方法 #setDefaults() 和 #setPriority() 都需要
    【解决方案3】:

    在通知生成器上添加setDefaults(Notification.DEFAULT_ALL) 对我有用。

    【讨论】:

      【解决方案4】:

      在您的代码中添加.setLargeIcon(R.mipmap.ic_launcher)

      Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                                                 R.drawable.yourIcon);
      
       Notification notify = new NotificationCompat.Builder(context)
                  .setAutoCancel(true)
                  .setContentTitle(title)
                  .setContentText(subject)
                  .setSmallIcon(R.mipmap.ic_launcher)
                  .setLargeIcon(icon)//can set only bitmap images convert your drawable to bitmap
                  .setNumber(count_unread_message)
                  .setPriority(0)
      
                  .setLights(Color.BLUE, 500, 500)
                  .setContentIntent(pendingIntent)
                  .build();
      

      https://developer.android.com/reference/android/app/Notification.html#largeIcon

      【讨论】:

      • 你确定问题是设置大图标吗?我收到此错误:Error:(224, 39) error: incompatible types: int cannot be converted to Bitmap
      • 不解决问题,我看不到通知,只能在状态栏上显示小通知图标,我在android 6.0.1上测试过,是这个问题的原因吗?还是必须设置任何权限?
      • .setLargeIcon(icon) 在 sdk v23 中已弃用,可能在 android 6.0.1 中不起作用,请查看上面的文档链接
      • 通知布局setLargeIcon 工作正常,但我不确定问题是setLargeIcon
      • 您好,我看到您编辑的帖子,谢谢,您不明白我的意思,先生,我再次编辑了。请再读一遍先生 :) 问题只显示在状态栏上的图标,我也想显示布局
      猜你喜欢
      • 1970-01-01
      • 2016-03-22
      • 1970-01-01
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      相关资源
      最近更新 更多