繁体   English   中英

Android通知仅在状态栏上显示图标

[英]Android notification shown only icon on status bar

在我的应用程序中,我编写了简单的通知功能,该方法仅在状态栏上显示图标,并且我也想显示通知布局,但没有自动显示,因此我已下拉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及更高版本

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

保罗

我相信您的问题是未显示通知横幅,对吗? 因此,用户必须降低状态栏才能看到通知。

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

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

参考: NotificationCompat.Builder文档

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

在您的代码中添加.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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM