/*** A notification with no importance: does not show in the shade.*/publicstatic final int IMPORTANCE_NONE = 0;/*** Min notification importance: only shows in the shade, below the fold.*/publicstatic final int IMPORTANCE_MIN = 1;/*** Low notification importance: shows everywhere, but is not intrusive.*/publicstatic final int IMPORTANCE_LOW = 2;/*** Default notification importance: shows everywhere, makes noise, but does not visually* intrude.*/publicstatic final int IMPORTANCE_DEFAULT = 3;/*** Higher notification importance: shows everywhere, makes noise and peeks. May use full screen* intents.*/publicstatic final int IMPORTANCE_HIGH = 4;/*** Unused.*/publicstatic final int IMPORTANCE_MAX = 5;
各種樣式的通知
下面給出各種式樣的通知的效果圖和寫法,可以根據實際需求選擇合適的方法,也可以自行組合。
簡單通知
效果圖
示例代碼
publicvoidsendSimpleNotification(Context context,NotificationManager nm){//創建點擊通知時發送的廣播Intent intent = new Intent(context,NotificationService.class);intent.setAction(ACTION_SIMPLE);PendingIntent pi = PendingIntent.getService(context,0,intent,0);//創建刪除通知時發送的廣播Intent deleteIntent = new Intent(context,NotificationService.class);deleteIntent.setAction(ACTION_DELETE);PendingIntent deletePendingIntent = PendingIntent.getService(context,0,deleteIntent,0);//創建通知Notification.Builder nb = new Notification.Builder(context,NotificationChannels.LOW)//設置通知左側的小圖標.setSmallIcon(R.mipmap.ic_notification)//設置通知標題.setContentTitle("Simple notification")//設置通知內容.setContentText("Demo for simple notification !")//設置點擊通知后自動刪除通知.setAutoCancel(true)//設置顯示通知時間.setShowWhen(true)//設置通知右側的大圖標.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_notifiation_big))//設置點擊通知時的響應事件.setContentIntent(pi)//設置刪除通知時的響應事件.setDeleteIntent(deletePendingIntent);//發送通知nm.notify(NOTIFICATION_SAMPLE,nb.build());}
帶Action按鈕通知
效果圖
示例代碼
publicvoidsendActionNotification(Context context,NotificationManager nm){//創建點擊通知時發送的廣播Intent intent = new Intent(context,NotificationService.class);intent.setAction(ACTION_ACTION);PendingIntent pi = PendingIntent.getService(context,0,intent,0);//創建通知Notification.Builder nb = new Notification.Builder(context,NotificationChannels.DEFAULT)//設置通知左側的小圖標.setSmallIcon(R.mipmap.ic_notification)//設置通知標題.setContentTitle("Action notification")//設置通知內容.setContentText("Demo for action notification !")//設置點擊通知后自動刪除通知.setAutoCancel(true)//設置顯示通知時間.setShowWhen(true)//設置點擊通知時的響應事件.setContentIntent(pi);//創建點擊通知 YES 按鈕時發送的廣播Intent yesIntent = new Intent(context, NotificationService.class);yesIntent.setAction(ACTION_YES);PendingIntent yesPendingIntent = PendingIntent.getService(context,0,yesIntent,0);Notification.Action yesActionBuilder = new Notification.Action.Builder(Icon.createWithResource("", R.mipmap.ic_yes),"YES",yesPendingIntent).build();//創建點擊通知 NO 按鈕時發送的廣播Intent noIntent = new Intent(context,NotificationService.class);noIntent.setAction(ACTION_NO);PendingIntent noPendingIntent = PendingIntent.getService(context,0,noIntent,0);Notification.Action noActionBuilder = new Notification.Action.Builder(Icon.createWithResource("", R.mipmap.ic_no),"NO",noPendingIntent).build();//為通知添加按鈕nb.setActions(yesActionBuilder,noActionBuilder);//發送通知nm.notify(NOTIFICATION_ACTION,nb.build());}
帶快速回復功能通知
效果圖
示例代碼
publicvoidsendRemoteInputNotification(Context context,NotificationManager nm){//創建點擊通知時發送的廣播Intent intent = new Intent(context,NotificationService.class);intent.setAction(ACTION_REMOTE_INPUT);PendingIntent pi = PendingIntent.getService(context,0,intent,0);//創建通知Notification.Builder nb = new Notification.Builder(context,NotificationChannels.IMPORTANCE)//設置通知左側的小圖標.setSmallIcon(R.mipmap.ic_notification)//設置通知標題.setContentTitle("Remote input notification")//設置通知內容.setContentText("Demo for remote input notification !")//設置點擊通知后自動刪除通知.setAutoCancel(true)//設置顯示通知時間.setShowWhen(true)//設置點擊通知時的響應事件.setContentIntent(pi);//創建帶輸入框的按鈕RemoteInput remoteInput = new RemoteInput.Builder(REMOTE_INPUT_RESULT_KEY).setLabel("Reply").build();Intent remoteInputIntent = new Intent(context,NotificationService.class);remoteInputIntent.setAction(ACTION_REPLY);PendingIntent replyPendingIntent = PendingIntent.getService(context,2,remoteInputIntent,0);Notification.Action replyAction = new Notification.Action.Builder(Icon.createWithResource("",R.mipmap.ic_reply),"Reply",replyPendingIntent).addRemoteInput(remoteInput).build();//為通知添加按鈕nb.setActions(replyAction);//發送通知nm.notify(NOTIFICATION_REMOTE_INPUT,nb.build());}
大圖效果通知(BigPictureStyle)
效果圖
示例代碼
publicvoidsendBigPictureStyleNotification(Context context,NotificationManager nm){//創建點擊通知時發送的廣播Intent intent = new Intent(context,NotificationService.class);intent.setAction(ACTION_BIG_PICTURE_STYLE);PendingIntent pi = PendingIntent.getService(context,0,intent,0);//創建大視圖樣式Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle().setBigContentTitle("Big picture style notification ~ Expand title").setSummaryText("Demo for big picture style notification ! ~ Expand summery").bigPicture(BitmapFactory.decodeResource(context.getResources(),R.mipmap.big_style_picture));//創建通知Notification.Builder nb = new Notification.Builder(context,NotificationChannels.DEFAULT)//設置通知左側的小圖標.setSmallIcon(R.mipmap.ic_notification)//設置通知標題.setContentTitle("Big picture style notification")//設置通知內容.setContentText("Demo for big picture style notification !")//設置點擊通知后自動刪除通知.setAutoCancel(true)//設置顯示通知時間.setShowWhen(true)//設置點擊通知時的響應事件.setContentIntent(pi)//設置大視圖樣式通知.setStyle(bigPictureStyle);//發送通知nm.notify(NOTIFICATION_BIG_PICTURE_STYLE,nb.build());}
多文字效果通知(BigTextStyle)
示例代碼
publicvoidsendBigTextStyleNotification(Context context,NotificationManager nm){//創建點擊通知時發送的廣播Intent intent = new Intent(context,NotificationService.class);intent.setAction(ACTION_BIG_TEXT_STYLE);PendingIntent pi = PendingIntent.getService(context,0,intent,0);//創建大文字樣式Notification.BigTextStyle bigTextStyle = new Notification.BigTextStyle().setBigContentTitle("Big text style notification ~ Expand title").setSummaryText("Demo for big text style notification ! ~ Expand summery").bigText("We are the champions \n" +"We are the champions \n" +"No time for losers \n" +"Cause we are the champions of the World");//創建通知Notification.Builder nb = new Notification.Builder(context,NotificationChannels.DEFAULT)//設置通知左側的小圖標.setSmallIcon(R.mipmap.ic_notification)//設置通知標題.setContentTitle("Big text style notification")//設置通知內容.setContentText("Demo for big text style notification !")//設置點擊通知后自動刪除通知.setAutoCancel(true)//設置顯示通知時間.setShowWhen(true)//設置點擊通知時的響應事件.setContentIntent(pi)//設置大文字樣式通知.setStyle(bigTextStyle);//發送通知nm.notify(NOTIFICATION_BIG_TEXT_STYLE,nb.build());}
信箱效果通知(InboxStyle)
效果圖
示例代碼
publicvoidsendInboxStyleNotification(Context context, NotificationManager nm){//創建點擊通知時發送的廣播Intent intent = new Intent(context,NotificationService.class);intent.setAction(ACTION_INBOX_STYLE);PendingIntent pi = PendingIntent.getService(context,0,intent,0);//創建信箱樣式Notification.InboxStyle inboxStyle = new Notification.InboxStyle().setBigContentTitle("Inbox style notification ~ Expand title").setSummaryText("Demo for inbox style notification ! ~ Expand summery")//最多六行.addLine("1. I am email content.").addLine("2. I am email content.").addLine("3. I am email content.").addLine("4. I am email content.").addLine("5. I am email content.").addLine("6. I am email content.");//創建通知Notification.Builder nb = new Notification.Builder(context,NotificationChannels.DEFAULT)//設置通知左側的小圖標.setSmallIcon(R.mipmap.ic_notification)//設置通知標題.setContentTitle("Inbox style notification")//設置通知內容.setContentText("Demo for inbox style notification !")//設置點擊通知后自動刪除通知.setAutoCancel(true)//設置顯示通知時間.setShowWhen(true)//設置點擊通知時的響應事件.setContentIntent(pi)//設置信箱樣式通知.setStyle(inboxStyle);//發送通知nm.notify(NOTIFICATION_INBOX_STYLE,nb.build());}
媒體效果通知(MediaStyle)
效果圖
示例代碼
publicvoidsendMediaStyleNotification(Context context, NotificationManager nm, boolean isPlaying){//創建點擊通知時發送的廣播Intent intent = new Intent(context,NotificationService.class);intent.setAction(ACTION_MEDIA_STYLE);PendingIntent pi = PendingIntent.getService(context,0,intent,0);//創建Action按鈕Intent playOrPauseIntent = new Intent(context,NotificationService.class);playOrPauseIntent.setAction(ACTION_MEDIA_STYLE);playOrPauseIntent.putExtra(EXTRA_OPTIONS, isPlaying ? MEDIA_STYLE_ACTION_PAUSE : MEDIA_STYLE_ACTION_PLAY);PendingIntent playOrPausePendingIntent = PendingIntent.getService(context,0, playOrPauseIntent,PendingIntent.FLAG_UPDATE_CURRENT);Notification.Action playOrPauseAction = new Notification.Action.Builder(Icon.createWithResource(context,isPlaying ? R.mipmap.ic_pause : R.mipmap.ic_play),isPlaying ? "PAUSE" : "PLAY",playOrPausePendingIntent).build();Intent nextIntent = new Intent(context,NotificationService.class);nextIntent.setAction(ACTION_MEDIA_STYLE);nextIntent.putExtra(EXTRA_OPTIONS, MEDIA_STYLE_ACTION_NEXT);PendingIntent nextPendingIntent = PendingIntent.getService(context,1, nextIntent,PendingIntent.FLAG_UPDATE_CURRENT);Notification.Action nextAction = new Notification.Action.Builder(Icon.createWithResource(context,R.mipmap.ic_next),"Next",nextPendingIntent).build();Intent deleteIntent = new Intent(context,NotificationService.class);deleteIntent.setAction(ACTION_MEDIA_STYLE);deleteIntent.putExtra(EXTRA_OPTIONS,MEDIA_STYLE_ACTION_DELETE);PendingIntent deletePendingIntent = PendingIntent.getService(context,2, deleteIntent,PendingIntent.FLAG_UPDATE_CURRENT);Notification.Action deleteAction = new Notification.Action.Builder(Icon.createWithResource(context,R.mipmap.ic_delete),"Delete",deletePendingIntent).build();//創建媒體樣式Notification.MediaStyle mediaStyle = new Notification.MediaStyle()//最多三個Action.setShowActionsInCompactView(0,1,2);//創建通知Notification.Builder nb = new Notification.Builder(context,NotificationChannels.MEDIA)//設置通知左側的小圖標.setSmallIcon(R.mipmap.ic_notification)//設置通知標題.setContentTitle("Media style notification")//設置通知內容.setContentText("Demo for media style notification !")//設置通知不可刪除.setOngoing(true)//設置顯示通知時間.setShowWhen(true)//設置點擊通知時的響應事件.setContentIntent(pi)//設置Action按鈕.setActions(playOrPauseAction,nextAction,deleteAction)//設置信箱樣式通知.setStyle(mediaStyle);//發送通知nm.notify(NOTIFICATION_MEDIA_STYLE,nb.build());}
信息效果通知(MessagingStyle)
效果圖
示例代碼
publicvoidsendMessagingStyleNotification(Context context, NotificationManager nm){//創建點擊通知時發送的廣播Intent intent = new Intent(context,NotificationService.class);intent.setAction(ACTION_MESSAGING_STYLE);PendingIntent pi = PendingIntent.getService(context,0,intent,0);//創建信息樣式Notification.MessagingStyle messagingStyle = new Notification.MessagingStyle("peter").setConversationTitle("Messaging style notification").addMessage("This is a message for you", System.currentTimeMillis(),"peter");//創建通知Notification.Builder nb = new Notification.Builder(context,NotificationChannels.DEFAULT)//設置通知左側的小圖標.setSmallIcon(R.mipmap.ic_notification)//設置通知標題.setContentTitle("Messaging style notification")//設置通知內容.setContentText("Demo for messaging style notification !")//設置點擊通知后自動刪除通知.setAutoCancel(true)//設置顯示通知時間.setShowWhen(true)//設置點擊通知時的響應事件.setContentIntent(pi)//設置信箱樣式通知.setStyle(messagingStyle);//發送通知nm.notify(NOTIFICATION_MESSAGING_STYLE,nb.build());}
帶進度條通知
效果圖
示例代碼
publicvoidsendProgressViewNotification(Context context,NotificationManager nm, int progress){//創建點擊通知時發送的廣播Intent intent = new Intent(context,NotificationService.class);intent.setAction(ACTION_PROGRESS);PendingIntent pi = PendingIntent.getService(context,0,intent,0);//創建通知Notification.Builder nb = new Notification.Builder(context,NotificationChannels.LOW)//設置通知左側的小圖標.setSmallIcon(R.mipmap.ic_notification)//設置通知標題.setContentTitle("Downloading...")//設置通知內容.setContentText(String.valueOf(progress) + "%")//設置通知不可刪除.setOngoing(true)//設置顯示通知時間.setShowWhen(true)//設置點擊通知時的響應事件.setContentIntent(pi).setProgress(100,progress,false);//發送通知nm.notify(NOTIFICATION_PROGRESS,nb.build());}
自定義頂部提醒視圖通知(CustomHeadsUpView)
效果圖
示例代碼
publicvoidsendCustomHeadsUpViewNotification(Context context, NotificationManager nm){//創建點擊通知時發送的廣播Intent intent = new Intent(context,LaunchActivity.class);PendingIntent pi = PendingIntent.getActivity(context,0,intent,0);//創建自定義頂部提醒視圖Intent answerIntent = new Intent(context,NotificationService.class);answerIntent.setAction(ACTION_CUSTOM_HEADS_UP_VIEW);answerIntent.putExtra(EXTRA_OPTIONS, ACTION_ANSWER);PendingIntent answerPendingIntent = PendingIntent.getService(context,0,answerIntent,PendingIntent.FLAG_UPDATE_CURRENT);Intent rejectIntent = new Intent(context,NotificationService.class);rejectIntent.setAction(ACTION_CUSTOM_HEADS_UP_VIEW);rejectIntent.putExtra(EXTRA_OPTIONS,ACTION_REJECT);PendingIntent rejectPendingIntent = PendingIntent.getService(context,1,rejectIntent,PendingIntent.FLAG_UPDATE_CURRENT);RemoteViews headsUpView = new RemoteViews(context.getPackageName(),R.layout.custom_heads_up_layout);headsUpView.setOnClickPendingIntent(R.id.iv_answer,answerPendingIntent);headsUpView.setOnClickPendingIntent(R.id.iv_reject,rejectPendingIntent);//創建通知Notification.Builder nb = new Notification.Builder(context,NotificationChannels.CRITICAL)//設置通知左側的小圖標.setSmallIcon(R.mipmap.ic_notification)//設置通知標題.setContentTitle("Custom heads up notification")//設置通知內容.setContentText("Demo for custom heads up notification !")//設置點擊通知后自動刪除通知.setAutoCancel(true)//設置顯示通知時間.setShowWhen(true)//設置點擊通知時的響應事件.setContentIntent(pi)//設置全屏響應事件;.setFullScreenIntent(pi,true)//設置自定義頂部提醒視圖.setCustomHeadsUpContentView(headsUpView);//發送通知nm.notify(NOTIFICATION_CUSTOM_HEADS_UP,nb.build());}
自定義視圖通知(CustomView)
效果圖
示例代碼
publicvoidsendCustomViewNotification(Context context, NotificationManager nm, NotificationContentWrapper content, Boolean isLoved, Boolean isPlaying){//創建點擊通知時發送的廣播Intent intent = new Intent(context,LaunchActivity.class);PendingIntent pi = PendingIntent.getActivity(context,0,intent,0);//創建各個按鈕的點擊響應廣播Intent intentLove = new Intent(context,NotificationService.class);intentLove.setAction(ACTION_CUSTOM_VIEW_OPTIONS_LOVE);PendingIntent piLove = PendingIntent.getService(context,0,intentLove,PendingIntent.FLAG_UPDATE_CURRENT);Intent intentPre = new Intent(context,NotificationService.class);intentPre.setAction(ACTION_CUSTOM_VIEW_OPTIONS_PRE);PendingIntent piPre = PendingIntent.getService(context,0,intentPre,PendingIntent.FLAG_UPDATE_CURRENT);Intent intentPlayOrPause = new Intent(context,NotificationService.class);intentPlayOrPause.setAction(ACTION_CUSTOM_VIEW_OPTIONS_PLAY_OR_PAUSE);PendingIntent piPlayOrPause = PendingIntent.getService(context,0, intentPlayOrPause,PendingIntent.FLAG_UPDATE_CURRENT);Intent intentNext = new Intent(context,NotificationService.class);intentNext.setAction(ACTION_CUSTOM_VIEW_OPTIONS_NEXT);PendingIntent piNext = PendingIntent.getService(context,0,intentNext,PendingIntent.FLAG_UPDATE_CURRENT);Intent intentLyrics = new Intent(context,NotificationService.class);intentLyrics.setAction(ACTION_CUSTOM_VIEW_OPTIONS_LYRICS);PendingIntent piLyrics = PendingIntent.getService(context,0,intentLyrics,PendingIntent.FLAG_UPDATE_CURRENT);Intent intentCancel = new Intent(context,NotificationService.class);intentCancel.setAction(ACTION_CUSTOM_VIEW_OPTIONS_CANCEL);PendingIntent piCancel = PendingIntent.getService(context,0,intentCancel,PendingIntent.FLAG_UPDATE_CURRENT);//創建自定義小視圖RemoteViews customView = new RemoteViews(context.getPackageName(),R.layout.custom_view_layout);customView.setImageViewBitmap(R.id.iv_content,content.bitmap);customView.setTextViewText(R.id.tv_title,content.title);customView.setTextViewText(R.id.tv_summery,content.summery);customView.setImageViewBitmap(R.id.iv_play_or_pause,BitmapFactory.decodeResource(context.getResources(),isPlaying ? R.mipmap.ic_pause : R.mipmap.ic_play));customView.setOnClickPendingIntent(R.id.iv_play_or_pause,piPlayOrPause);customView.setOnClickPendingIntent(R.id.iv_next,piNext);customView.setOnClickPendingIntent(R.id.iv_lyrics,piLyrics);customView.setOnClickPendingIntent(R.id.iv_cancel,piCancel);//創建自定義大視圖RemoteViews customBigView = new RemoteViews(context.getPackageName(),R.layout.custom_big_view_layout);customBigView.setImageViewBitmap(R.id.iv_content_big,content.bitmap);customBigView.setTextViewText(R.id.tv_title_big,content.title);customBigView.setTextViewText(R.id.tv_summery_big,content.summery);customBigView.setImageViewBitmap(R.id.iv_love_big,BitmapFactory.decodeResource(context.getResources(),isLoved ? R.mipmap.ic_loved : R.mipmap.ic_love));customBigView.setImageViewBitmap(R.id.iv_play_or_pause_big,BitmapFactory.decodeResource(context.getResources(),isPlaying ? R.mipmap.ic_pause : R.mipmap.ic_play));customBigView.setOnClickPendingIntent(R.id.iv_love_big,piLove);customBigView.setOnClickPendingIntent(R.id.iv_pre_big,piPre);customBigView.setOnClickPendingIntent(R.id.iv_play_or_pause_big,piPlayOrPause);customBigView.setOnClickPendingIntent(R.id.iv_next_big,piNext);customBigView.setOnClickPendingIntent(R.id.iv_lyrics_big,piLyrics);customBigView.setOnClickPendingIntent(R.id.iv_cancel_big,piCancel);//創建通知Notification.Builder nb = new Notification.Builder(context,NotificationChannels.MEDIA)//設置通知左側的小圖標.setSmallIcon(R.mipmap.ic_notification)//設置通知標題.setContentTitle("Custom notification")//設置通知內容.setContentText("Demo for custom notification !")//設置通知不可刪除.setOngoing(true)//設置顯示通知時間.setShowWhen(true)//設置點擊通知時的響應事件.setContentIntent(pi)//設置自定義小視圖.setCustomContentView(customView)//設置自定義大視圖.setCustomBigContentView(customBigView);//發送通知nm.notify(NOTIFICATION_CUSTOM,nb.build());}