为什么Android通知没有显示我设置的图标?

时间:2018-04-11 06:56:22

标签: android notifications

我正在关注谷歌Android开发Android练习的基础教程。当涉及到通知时,我没有设置显示在通知左侧的图标。 教程是here

public class MainActivity extends AppCompatActivity {

private ToggleButton mToggleButton;
private NotificationManager notificationManager;
private static final int NOTIFICATION_ID=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mToggleButton= findViewById(R.id.alarmToggle);
    //改变togglebutton文字
  //mToggleButton.setTextOff("Off");
//  mToggleButton.setTextOn("On");
//  mToggleButton.setText("Off");
    mToggleButton.setOnCheckedChangeListener(new 
CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            String toastMessage;
            if (b){
                toastMessage=getString(R.string.toggleButton_on);
                deliverNotification(MainActivity.this);
            }
            else {
                toastMessage=getString(R.string.toggleButton_off);
                notificationManager.cancelAll();
            }
            Toast.makeText(MainActivity.this,toastMessage,Toast.LENGTH_SHORT).show();
        }
    });
}

public void deliverNotification(Context context){

    Intent notificationIntent = new Intent(context,MainActivity.class);
    PendingIntent notificationPendingIntent =PendingIntent.getActivity(
            context,NOTIFICATION_ID,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);

//    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"StandUp")
            .setSmallIcon(R.drawable.ic_run)
            //这里设定的图标是AS里随便找的一个,设置了没效果
            //this doesn't work.ic_run comes from AS notifications image asset.
            .setContentTitle(getString(R.string.notification_title))
            .setContentText(getString(R.string.notification_text))
            .setContentIntent(notificationPendingIntent)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setAutoCancel(true);

//        notificationManager.notify(NOTIFICATION_ID,builder.build());
    notificationManager.notify(NOTIFICATION_ID,builder.build());
    }
}

结果如下:通知的左侧是默认的android启动器

enter image description here

更新: 这次我遵循lesson。我将通知设置更改为:

        mNotifyManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("Battery Alert!")
            .setContentText("Your battery is out of use.")
            .setContentIntent(notificationPendingIntent)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .addAction(R.drawable.ic_stat_name,"Learn More",learnMorePendingIntent)
            .addAction(R.drawable.ic_update,"Update",updatePendingIntent)
            .setAutoCancel(true)
            .setDeleteIntent(dismissPendingIntent);//设置划掉通知产生的广播
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
        notifyBuilder.setSmallIcon(R.drawable.ic_battery);
        notifyBuilder.setColor(getResources().getColor(R.color.colorAccent));
    }
    else{
        notifyBuilder.setSmallIcon(R.drawable.ic_battery);
    }

这不起作用。 Only the text color of action button changed to the color I set

图标: ic_battery ic_battery 我错过了什么?

1 个答案:

答案 0 :(得分:0)

您应该根据API版本设置通知图标。

Notification notification = new NotificationCompat.Builder(this);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    notification.setSmallIcon(R.drawable.icon_transperent);
    notification.setColor(getResources().getColor(R.color.notification_color));
} else { 
    notification.setSmallIcon(R.drawable.icon);
} 

请参阅此处:https://stackoverflow.com/a/30795471/2147481