本項目展現了經常使用的Android系統自帶的Notification,以及總結了一些本身在使用notification的過程當中遇到的坑。java
首先來看一下常見的notification type:
public static final int TYPE_NORMAL = 1; // 普統統知
public static final int TYPE_PROGRESS = 2; // 下載進度的通知
public static final int TYPE_BIG_TEXT = 3; // BigTextStyle通知
public static final int TYPE_INBOX = 4; // InboxStyle
public static final int TYPE_BIG_PICTURE = 5; // BigPictureStyle
public static final int TYPE_HANGUP = 6; // hangup橫幅通知
複製代碼
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//爲了版本兼容 選擇V7包下的NotificationCompat進行構造
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//Ticker是狀態欄顯示的提示
builder.setTicker("簡單Notification");
//第一行內容 一般做爲通知欄標題
builder.setContentTitle("標題");
//第二行內容 一般是通知正文
builder.setContentText("通知內容");
//第三行內容 一般是內容摘要什麼的 在低版本機器上不必定顯示
builder.setSubText("這裏顯示的是通知第三行內容!");
//ContentInfo 在通知的右側 時間的下面 用來展現一些其餘信息
//builder.setContentInfo("2");
//number設計用來顯示同種通知的數量和ContentInfo的位置同樣,若是設置了ContentInfo則number會被隱藏
builder.setNumber(2);
//能夠點擊通知欄的刪除按鈕刪除
builder.setAutoCancel(true);
//系統狀態欄顯示的小圖標
builder.setSmallIcon(R.mipmap.ic_launcher);
//下拉顯示的大圖標
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.contact));
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent, 0);
//點擊跳轉的intent
builder.setContentIntent(pIntent);
//通知默認的聲音 震動 呼吸燈
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
Notification notification = builder.build();
manager.notify(TYPE_NORMAL, notification);
複製代碼
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.contact));
//禁止用戶點擊刪除按鈕刪除
builder.setAutoCancel(false);
//禁止滑動刪除
builder.setOngoing(true);
//取消右上角的時間顯示
builder.setShowWhen(false);
builder.setContentTitle("下載中...7%");
builder.setProgress(100, 7, false);
//builder.setContentInfo(progress+"%");
builder.setOngoing(true);
builder.setShowWhen(false);
Intent intent = new Intent(this, NotificationActivity.class);
intent.putExtra("command", 1);
PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent, 0);
//點擊跳轉的intent
builder.setContentIntent(pIntent);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
Notification notification = builder.build();
manager.notify(TYPE_PROGRESS, notification);
複製代碼
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("BigTextStyle");
builder.setContentText("BigTextStyle演示示例");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.contact));
android.support.v4.app.NotificationCompat.BigTextStyle style = new android.support.v4.app.NotificationCompat.BigTextStyle();
style.bigText("這裏是點擊通知後要顯示的正文,能夠換行能夠顯示很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長");
style.setBigContentTitle("點擊後的標題");
//SummaryText沒什麼用 能夠不設置
style.setSummaryText("末尾只一行的文字內容");
builder.setStyle(style);
builder.setAutoCancel(true);
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent, 0);
builder.setContentIntent(pIntent);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
Notification notification = builder.build();
manager.notify(TYPE_BIG_TEXT, notification);
複製代碼
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("InboxStyle");
builder.setContentText("InboxStyle演示示例");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.contact));
android.support.v4.app.NotificationCompat.InboxStyle style = new android.support.v4.app.NotificationCompat.InboxStyle();
style.setBigContentTitle("BigContentTitle")
.addLine("第一行,第一行,第一行,第一行,第一行,第一行,第一行")
.addLine("第二行")
.addLine("第三行")
.addLine("第四行")
.addLine("第五行")
.setSummaryText("SummaryText");
builder.setStyle(style);
builder.setAutoCancel(true);
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent, 0);
builder.setContentIntent(pIntent);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
Notification notification = builder.build();
manager.notify(TYPE_INBOX, notification);
複製代碼
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("BigPictureStyle");
builder.setContentText("BigPicture演示示例");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.contact));
android.support.v4.app.NotificationCompat.BigPictureStyle style = new android.support.v4.app.NotificationCompat.BigPictureStyle();
style.setBigContentTitle("BigContentTitle");
style.setSummaryText("SummaryText");
style.bigPicture(BitmapFactory.decodeResource(getResources(), R.mipmap.img_power_popup_notice));
builder.setStyle(style);
builder.setAutoCancel(true);
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent, 0);
//設置點擊大圖後跳轉
builder.setContentIntent(pIntent);
Notification notification = builder.build();
manager.notify(TYPE_BIG_PICTURE, notification);
複製代碼
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("橫幅通知");
builder.setContentText("請在設置通知管理中開啓消息橫幅提醒權限");
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.contact));
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent, 0);
builder.setContentIntent(pIntent);
//這句是重點
builder.setFullScreenIntent(pIntent, true);
builder.setAutoCancel(true);
Notification notification = builder.build();
manager.notify(TYPE_HANGUP, notification);
複製代碼
以上的例子中,連續發送多條notification,你會發現全部的notification都只展現了最新的一條,以前發送的都會被最後一條頂替。假如你想要展現全部的
notification,應該怎麼處理呢?只要將
複製代碼
manager.notify(TYPE_HANGUP, notification);
複製代碼
這個方法中的第一個參數id,改爲不一樣值,就可讓不一樣的notification全展現了。
複製代碼
當你發送橫幅通知的時候你會發現橫幅沒有自動收起,我採用了下面的方法達到了自動收起的效果
複製代碼
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
manager.cancel(TYPE_HANGUP);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
builder.setContentTitle("橫幅通知");
builder.setContentText("請在設置通知管理中開啓消息橫幅提醒權限");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.contact));
Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0);
builder.setContentIntent(pIntent);
builder.setAutoCancel(true);
Notification notification = builder.build();
manager.notify(TYPE_HANGUP, notification);
}
}, 2000);
複製代碼
延遲兩秒去cancel這個notification,可是又會發現通知欄裏又沒有了這個notification,此時再悄無聲息的再發送一次,完美解決。
複製代碼
這個坑很蛋疼,當你調用了這個方法 builder.setFullScreenIntent(pIntent, true); 而且你的app沒有退出的時候,你會發如今某些手機上,你的app會自動
跳轉到你notification裏設置的那個頁面,即便在息屏的狀態下也會。修復的辦法只要將上面的代碼改成 builder.setFullScreenIntent(null, true); 便可。
複製代碼
當你發送多條notification,而且每條notification所攜帶的參數(即想要打開的指定的頁面)不一樣,你會發現intent裏攜帶的參數會被最新的一條notification
裏帶的參數替換,致使老是打開同一個頁面。修正的方法是將 PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0);
這句代碼裏的第二個參數 requestCode 設置成不一樣的值,便可將不一樣notification intent所攜帶的參數存住。
複製代碼
That's all, I hope this will help you!android
附:源碼地址git
展現不一樣的notification,就須要不一樣的id,使用同一個id,新的notification中的內容會去更新老notification的內容,而不是替換。
複製代碼
當你設置了 builder.setFullScreenIntent(pIntent, true); 這句代碼時,notification是默認懸浮在手機頂部待處理的。我這裏作的讓它自動收回的效果(假象)只是產品的特殊需求。
複製代碼
關於這個問題的正解,建議看一下這篇文章,你會獲得答案。github