我已经实现了以下方法来在 android 中显示一些通知。该方法与 developer.android.com 教程非常相似,但我的通知不会显示,无论是在 4.1 上还是在 4.0 或 2.3 上。
public void showNotification() {
String appname = "App Name";
String title = "Notification Title";
String text = "This is the notification text";
String iconUrl = "http://url.to.image.com/image.png";
Bitmap largeIcon = imageCache.getBitmap(iconUrl);
NotificationManager notifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
TaskStackBuilder stackBuilder = TaskStackBuilder.from(context);
stackBuilder.addParentStack(NewsActivity.class);
stackBuilder.addNextIntent(new Intent(context, NewsActivity.class));
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentTitle(title).setContentInfo(appname).setLargeIcon(largeIcon).setContentText(text).setContentIntent(pendingIntent);
notifyManager.notify("textid", 123, builder.getNotification());
}