【发布时间】:2016-07-17 14:26:34
【问题描述】:
我刚刚注意到,当我创建通知时,在其他版本为 4.4 及以下版本的手机上,应用程序图标上没有显示通知徽章,只有通知栏中的通知。
我想在各种安卓版本的应用图标旁边显示红色通知徽章,就像下面的截图一样:
这是我用来显示通知的代码,基本上用户会在通知栏上收到通知,但不会收到应用程序中的红色徽章编号:
private void sendNotification(NotificationVM source) {
Intent intent = new Intent(this, NotificationActivity.class);
intent.putExtra(IntentPutNameConstant.NotificationActivity.NOTIFICATION_ID, source.getNotificationId());
intent.putExtra(IntentPutNameConstant.NotificationActivity.NOTIFICATION_TYPE, source.getNotificationType());
intent.putExtra(IntentPutNameConstant.NotificationActivity.USER_FROM_USERNAME, source.getUserFrom().getUsername());
intent.putExtra(IntentPutNameConstant.NotificationActivity.MESSAGE, source.getMessage());
intent.putExtra(IntentPutNameConstant.NotificationActivity.SOURCE_ID, source.getSourceId());
intent.putExtra(IntentPutNameConstant.NotificationActivity.EXTERNAL_URL, source.getExternalUrl());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_app_launcher_72)
.setContentTitle("Investagrams")
.setContentText(source.getMessage())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
//Vibration
notificationBuilder.setVibrate(new long[] { 0, 200, 200, 200, 200, 200 });
//LED
//notificationBuilder.setLights(Color.RED, 3000, 3000);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = notificationBuilder.build();
notif.flags |= Notification.FLAG_AUTO_CANCEL;
/*notif.ledARGB = 0xFFff0000;
notif.flags = Notification.FLAG_SHOW_LIGHTS;
notif.ledOnMS = 100;
notif.ledOffMS = 100;*/
notificationManager.notify((int)source.getNotificationId() /* ID of notification */, notif);
}
【问题讨论】:
-
这是启动器的工作,与您应用的通知无关。例如 Nova Launcher 可以显示通知计数,但您将依赖具有通知计数显示机制的用户。
标签: android push-notification notifications google-cloud-messaging