我正在使用代码:
/** Prepare the notification UI **/
private void prepareNotificationUI(String mTitle, String mDescription, int notificationId, Bundle bundle) {
Intent intent = new Intent(mContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setAction(Long.toString(System.currentTimeMillis()));
intent.putExtras(bundle);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationHelper notificationHelper = new NotificationHelper(getApplicationContext());
nb = notificationHelper.getNotification(pendingIntent, mTitle, mDescription);
if (nb != null) {
notificationHelper.notify(notificationId, nb);
}
}else {
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
String channelId = mContext.getString(R.string.app_name);
Bitmap largeIcon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.notification_icon);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext, channelId);
//Notification.Builder notificationBuilder=new Notification.Builder(mContext);
notificationBuilder.setContentTitle(mTitle);
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(mDescription));
notificationBuilder.setContentText(mDescription);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(defaultSoundUri);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.drawable.notification_icon);
notificationBuilder.setColor(ContextCompat.getColor(mContext, R.color.white));
notificationBuilder.setLargeIcon(largeIcon);
} else {
notificationBuilder.setSmallIcon(R.drawable.notification_icon);
notificationBuilder.setLargeIcon(largeIcon);
}
/*notificationBuilder.setSmallIcon(R.drawable.notification_icon);
notificationBuilder.setColor(ContextCompat.getColor(mContext, R.color.white));*/
notificationBuilder.setDefaults(Notification.DEFAULT_ALL);
notificationBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
notificationBuilder.setLights(Color.WHITE, 3000, 3000);
notificationBuilder.setContentIntent(pendingIntent);
notificationManager.notify(notificationId, notificationBuilder.build());
}
}
-
我在Marshmellow设备中得到的图标非常小。很好用
-
早些时候,这个图标根本没有在Marshmellow中显示出来。然后我
添加了下面的代码。图标正在显示,但大小很小
notificationBuilder.setLargeIcon文件(largeIcon公司);