1.作用:Notification使用非常普遍,几乎没个APP都会设置相关的功能,与推送相似,是与用户交互的重要方式。
2.用法:直接new Notification,再进行设置的方式目前已经不推荐使用了,现在应使用Builder来进行一系列的设置,基本方法及设置如下:
//发送通知
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, new Intent(this,MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
//1.初始化一个Builder对象
NotificationCompat.Builder notifyBuilder=new NotificationCompat.Builder(this);
//2.通过Builder进行一系列的set方法
notifyBuilder.setContentTitle("通知标题");
notifyBuilder.setContentText("通知内容");
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.map_3d_normal);
notifyBuilder.setSmallIcon(R.drawable.ic_launcher);
notifyBuilder.setLargeIcon(bitmap);
notifyBuilder.setNumber(1);
notifyBuilder.setWhen(System.currentTimeMillis());
notifyBuilder.setContentIntent(pendingIntent);
//3.其他设置
notifyBuilder.setAutoCancel(true);
notifyBuilder.setOngoing(true);
notifyBuilder.setTicker("通知来了");
NotificationManager mNotificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, notifyBuilder.build());
3.注意事项:setContentTitile setContentText setSmallIcon 这三个属性的设置是必须的,否则手机无法收到通知,PendingIntent的详解将在下一次解释