专案网址https://github.com/uuko/android_project/tree/master/Notifaction
参考文章 android官方document+codelab
每个通知通道代表一种通知类型。
在您的代码中,您可以在每个通知通道中将多个通知分组。
对于每个通知通道,您的应用程序设置行为的通道,并且行为被应用到该频道的所有通知。例如,您的应用可能会在频道中设置通知以播放声音,闪烁灯光或振动。
无论您的应用为通知渠道设置了什么行为,用户都可以更改该行为,并且用户可以完全关闭您的应用的通知。
//每个channel与唯一id相连 private static final String PRIMARY_CHANNEL_ID ="primary_notification_channel"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button notify=(Button) findViewById(R.id.notify); notify.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendNotification(); } }); ** createNotificationChannel();** } public void sendNotification(){ //builder实例 NotificationCompat.Builder notifyBuilder = getNotificationBuilder(); notificationManager.**notify**(NOTIFICATION_ID, notifyBuilder.build()); } //notificationmanger >传递给用户的 private NotificationManager notificationManager;public void createNotificationChannel() {//实体化manager并设置一些参数 notificationManager= (NotificationManager)getSystemService(NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.O){ /*创建一个channel对象 public NotificationChannel (String id, CharSequence name, int importance)*/ NotificationChannel notificationChannel= new NotificationChannel(PRIMARY_CHANNEL_ID, "123" ,NotificationManager.IMPORTANCE_HIGH ); notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.RED); notificationChannel.enableVibration(true); notificationChannel.setDescription("hihi"); //正式创建 notificationManager.createNotificationChannel(notificationChannel); }}//使用NotificationCompat.Builder该类创建通知//需要一个ID让通知相关 以好取消OR更新private static final int NOTIFICATION_ID = 0;private NotificationCompat.Builder getNotificationBuilder(){ Intent notifyIntent=new Intent(this,MainActivity.class); //intent必须包在pendingintent里面 PendingIntent notifyPendingIntent=PendingIntent.getActivity(this, NOTIFICATION_ID,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT); //FLAG_UPDATE_CURRENT>第二条会替换第一条内容 CANCEL是会变点第一条没反应只有第二条可以点 NotificationCompat.Builder notifyBuilder= new NotificationCompat.Builder(this,PRIMARY_CHANNEL_ID) .setContentTitle("notify~~~") .setContentText("uuko notify!!") .setSmallIcon(R.drawable.ic_launcher_background) .setContentIntent(notifyPendingIntent)//加入intent .setAutoCancel(true); return notifyBuilder; }
观念>
通知为您的应用提供了一种与用户进行交互的方式,即使该应用未运行也是如此。
当Android发布通知时,该通知首先显示为设备通知区域中的图标。
(这可以用intent+pendingintent去做 再加broadcast)
要指定通知的UI和操作,请使用NotificationCompat.Builder。
要创建通知,请使用NotificationCompat.Builder.build()。
要发出通知,请使用NotificationManager.notify()将该通知对像传递给Android运行时系统。
为了使更新或取消通知成为可能,请将通知ID与通知相关联。
通知可以包含多个组件,包括一个小图标(setSmallIcon(),必填);标题(setContentTitle()); 以及详细的文字(setContentText())。
通知还可以包括待处理的意图,扩展的样式,优先级等。有关更多详细信息,请参见 NotificationCompat.Builder。
//功能拓展
public class MainActivity extends AppCompatActivity { private static final String PRIMARY_CHANNEL_ID = "primary_notification_channel"; private NotificationManager notificationManager; private static final int NOTIFICATION_ID = 0; private static final String ACTION_UPDATE_NOTIFICATION = "com.example.android.Notifaction.ACTION_UPDATE_NOTIFICATION"; private NotificationReciever notificationReciever; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); notificationReciever=new NotificationReciever(); Button update=(Button) findViewById(R.id.update); update.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { updateNotification(); } }); Button cancel=(Button) findViewById(R.id.cancel); cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cancelupdateNotification(); } }); Button notify=(Button) findViewById(R.id.notify); notify.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendNotification(); } }); createNotificationChannel(); setbuttonstate(true,false,false); registerReceiver(notificationReciever,new IntentFilter(ACTION_UPDATE_NOTIFICATION)); } @Override public void onDestroy(){ unregisterReceiver(notificationReciever); super.onDestroy(); } //更新 public void updateNotification(){ Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.mascot_1); NotificationCompat.Builder notifybuilder=getNotificationBuilder(); notifybuilder.setStyle(new NotificationCompat.BigPictureStyle() .bigPicture(bitmap) .setBigContentTitle("bigggg")); notificationManager.notify(NOTIFICATION_ID,notifybuilder.build()); setbuttonstate(false,true,true); } //取消通知 public void cancelupdateNotification(){ notificationManager.cancel(NOTIFICATION_ID); } public void sendNotification(){ Intent intent=new Intent(ACTION_UPDATE_NOTIFICATION); PendingIntent pendingIntent=PendingIntent.getBroadcast(this,NOTIFICATION_ID,intent,PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder notifyBuilder = getNotificationBuilder(); notifyBuilder.addAction(R.drawable.ic_update,"update",pendingIntent); notificationManager.notify(NOTIFICATION_ID, notifyBuilder.build()); setbuttonstate(false,true,true); } void setbuttonstate(Boolean isNotifyEnabled,Boolean isUpdadateEnabled,Boolean isCancelEnabled){ Button update=(Button) findViewById(R.id.update); Button notify=(Button) findViewById(R.id.notify); Button cancel=(Button) findViewById(R.id.cancel); notify.setEnabled(isNotifyEnabled); update.setEnabled(isUpdadateEnabled); cancel.setEnabled(isCancelEnabled); } public void createNotificationChannel(){ notificationManager= (NotificationManager)getSystemService(NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.O){ NotificationChannel notificationChannel= new NotificationChannel(PRIMARY_CHANNEL_ID, "123" ,NotificationManager.IMPORTANCE_HIGH ); notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.RED); notificationChannel.enableVibration(true); notificationChannel.setDescription("hihi"); notificationManager.createNotificationChannel(notificationChannel); } } private NotificationCompat.Builder getNotificationBuilder(){ Intent notifyIntent=new Intent(this,MainActivity.class); PendingIntent notifyPendingIntent=PendingIntent.getActivity(this, NOTIFICATION_ID,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder notifyBuilder= new NotificationCompat.Builder(this,PRIMARY_CHANNEL_ID) .setContentTitle("notify~~~") .setContentText("uuko notify!!") .setSmallIcon(R.drawable.ic_launcher_background) .setContentIntent(notifyPendingIntent) .setAutoCancel(true); return notifyBuilder; } public class NotificationReciever extends BroadcastReceiver{ public NotificationReciever(){ } @Override public void onReceive(Context context, Intent intent) { updateNotification(); } }}