如果缺少 setSmallIcon,为什么 Android 通知不起作用?
Why Android Notification not works if setSmallIcon is Missing?
我的疑问是,如果代码中缺少 setSmallIcon,为什么不构建通知:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// builder.setSmallIcon(R.drawable.ic_launcher);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
builder.setContentTitle("Google");
builder.setContentText("Go to Google");
builder.setSubText("Tap to go to link in notifications.");
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
app 不会抛出任何异常,代码 运行 没问题,但通知不会显示,如果我取消注释
builder.setSmallIcon(R.drawable.ic_launcher);
app 通知工作正常,所以我的问题是为什么 builder.setSmallIcon
必须显示通知。
如果我不想在状态栏上显示小图标,有什么解决方案吗?
因为.setSmallIcon(R.drawable.ic_launcher);
这是先决条件。如果您使用 NotificationCompat.Builder
。我在 Google 开发者博客中阅读了一些内容。
我的疑问是,如果代码中缺少 setSmallIcon,为什么不构建通知:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// builder.setSmallIcon(R.drawable.ic_launcher);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
builder.setContentTitle("Google");
builder.setContentText("Go to Google");
builder.setSubText("Tap to go to link in notifications.");
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
app 不会抛出任何异常,代码 运行 没问题,但通知不会显示,如果我取消注释
builder.setSmallIcon(R.drawable.ic_launcher);
app 通知工作正常,所以我的问题是为什么 builder.setSmallIcon
必须显示通知。
如果我不想在状态栏上显示小图标,有什么解决方案吗?
因为.setSmallIcon(R.drawable.ic_launcher);
这是先决条件。如果您使用 NotificationCompat.Builder
。我在 Google 开发者博客中阅读了一些内容。