[英]Android: The notification does not appear on the screen, only the icon in the status bar Oreo
[英]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
在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.