一般通知
以下三個(gè)方法必須調(diào)用
mBulider.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, new Intent(this, ResultActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setContentIntent(contentIntent);
mNotifyMgr.notify(NOTIFICATIONS_ID, mBuilder.build());
進(jìn)度條通知
明確進(jìn)度的進(jìn)度條
使用setProgress(max, progress, false)來(lái)更新進(jìn)度。
max: 最大進(jìn)度值
progress: 當(dāng)前進(jìn)度
false: 是否是不明確的進(jìn)度條
int id = 1;
...
mNotifyManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Picture Download")
.setContentText("Download in progress")
.setSmallIcon(R.drawable.ic_notification);
// Start a lengthy operation in a background thread
new Thread(
new Runnable() {
@Override
public void run() {
int incr;
for (incr = 0; incr <= 100; incr+=5) {
mBuilder.setProgress(100, incr, false);
mNotifyManager.notify(id, mBuilder.build());
try {
// Sleep for 5 seconds
Thread.sleep(5*1000);
} catch (InterruptedException e) {
Log.d(TAG, "sleep failure");
}
}
mBuilder.setContentText("Download complete")//下載完成
.setProgress(0,0,false); //移除進(jìn)度條
mNotifyManager.notify(id, mBuilder.build());
}
}
).start();
自定義通知
自定義普通視圖通知高度限制為64dp磕潮,大視圖通知高度限制為256dp弧可。自定義通知需要做如下操作:
1捷泞、創(chuàng)建自定義通知布局
2秉氧、使用RemoteViews定義通知組件址儒,如圖標(biāo)僻焚、文字等
3允悦、調(diào)用setContent()將RemoteViews對(duì)象綁定到NotificationCompat.Builder
4、同正常發(fā)送通知流程
RemoteViews bigView;
RemoteViews smallView;
// 構(gòu)建 bigView 和 smallView溅呢。
...
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
// 設(shè)置自定義 RemoteViews
builder.setContent(smallView).setSmallIcon(R.drawable.icon_notification);
Notification notification = builder.build();
// 如果系統(tǒng)版本 >= Android 4.1澡屡,設(shè)置大視圖 RemoteViews
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification.bigContentView = bigView;
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(DAILY_PUSH_NOTIFICATION_ID, notification);
浮動(dòng)通知
以下兩種情況會(huì)顯示浮動(dòng)通知:
-
setFullScreenIntent()
NotificationCompat.Builder mNotifyBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setFullScreenIntent(pendingIntent, false);
> - 需要設(shè)置 Notification 的優(yōu)先級(jí)為 PRIORITY_HIGH 或者 PRIORITY_MAX 并且使用鈴聲或振動(dòng)
// 設(shè)置通知的優(yōu)先級(jí)
builder.setPriority(NotificationCompat.PRIORITY_MAX);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// 設(shè)置通知的提示音
builder.setSound(alarmSound);
// 設(shè)置通知的優(yōu)先級(jí)
mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
// Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// // 設(shè)置通知的提示音
// mBuilder.setSound(alarmSound);
mBuilder.setDefaults(Notification.DEFAULT_ALL);
## 鎖屏通知
> Android 5.0(API level 21)開(kāi)始,通知可以顯示在鎖屏上咐旧。你的應(yīng)用可以通過(guò)setVisibility()控制通知的顯示等級(jí)
- VISIBILITY_PRIVATE : 顯示基本信息驶鹉,如通知的圖標(biāo),但隱藏通知的全部?jī)?nèi)容
- VISIBILITY_PUBLIC : 顯示通知的全部?jī)?nèi)容
- VISIBILITY_SECRET : 不顯示任何內(nèi)容铣墨,包括圖標(biāo)
#?? 注意
**經(jīng)測(cè)試室埋,針對(duì)浮動(dòng)通知與鎖屏通知,三星手機(jī)有效,華為手機(jī)在于手機(jī)通知中心設(shè)置姚淆,不在代碼孕蝉。**
> - 華為手機(jī)5.0系統(tǒng)與6.0系統(tǒng) 通知中心 可以設(shè)置通知方式,默認(rèn)狀態(tài)欄通知方式打開(kāi)腌逢,橫幅(浮動(dòng)通知)方式關(guān)閉和鎖屏(鎖屏通知)關(guān)閉降淮,則調(diào)用以上代碼無(wú)效
- 三星手機(jī)調(diào)用有效
## 通知更新設(shè)置
> 1. 再次發(fā)送相同ID的通知,如果之前的通知依然存在則會(huì)更新通知屬性搏讶,如果之前通知不存在則重新創(chuàng)建
2. 設(shè)置了 setAutoCancel(true) 佳鳖,點(diǎn)擊該通知時(shí)會(huì)清除它
3. 通過(guò) NotificationManager 調(diào)用 cancel() 方法清除指定ID的通知
4. 通過(guò) NotificationManager 調(diào)用 cancelAll() 方法清除所有該應(yīng)用之前發(fā)送的通知
5. 點(diǎn)擊通知欄的清除按鈕,會(huì)清除所有可清除的通知
## 通知效果設(shè)置
1. 對(duì)于鈴聲媒惕、閃光系吩、震動(dòng)這三個(gè)屬性,NotificationCompat.Builder提供了三個(gè)方法設(shè)定:
- setSound(Uri sound):設(shè)定一個(gè)鈴聲妒蔚,用于在通知的時(shí)候響應(yīng)穿挨。傳遞一個(gè)Uri的參數(shù),格式為“file:///mnt/sdcard/Xxx.mp3”肴盏。
- setLights(int argb, int onMs, int offMs):設(shè)定前置LED燈的閃爍速率科盛,持續(xù)毫秒數(shù),停頓毫秒數(shù)叁鉴。
- setVibrate(long[] pattern):設(shè)定震動(dòng)的模式土涝,以一個(gè)long數(shù)組保存毫秒級(jí)間隔的震動(dòng)。
2. 一般使用setDefaults(int)方法設(shè)定默認(rèn)響應(yīng)參數(shù)
- DEFAULT_ALL:鈴聲幌墓、閃光但壮、震動(dòng)均系統(tǒng)默認(rèn)
- DEFAULT_SOUND:系統(tǒng)默認(rèn)鈴聲
- DEFAULT_VIBRATE:系統(tǒng)默認(rèn)震動(dòng)
- DEFAULT_LIGHTS:系統(tǒng)默認(rèn)閃光
3. 而在Android中,如果需要訪問(wèn)硬件設(shè)備的話常侣,是需要對(duì)其進(jìn)行授權(quán)的蜡饵,所以需要在清單文件AndroidManifest.xml中增加兩個(gè)授權(quán),分別授予訪問(wèn)振動(dòng)器與閃光燈的權(quán)限:
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.VIBRATE"/>
## Activity返回棧
### 常規(guī)Activity
> 默認(rèn)情況下胳施,從通知啟動(dòng)一個(gè)Activity溯祸,按返回鍵會(huì)回到主屏幕。但某些時(shí)候有按返回鍵仍然留在當(dāng)前應(yīng)用的需求舞肆,這就要用到TaskStackBuilder了焦辅。
1、在manifest中定義Activity的關(guān)系
Android 4.0.3 及更早版本
<activity
android:name=".ResultActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
Android 4.1 及更高版本
<activity
android:name=".ResultActivity"
android:parentActivityName=".MainActivity">
</activity>
2椿胯、創(chuàng)建返回棧PendingIntent
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// 添加返回棧
stackBuilder.addParentStack(ResultActivity.class);
// 添加Intent到棧頂
stackBuilder.addNextIntent(resultIntent);
// 創(chuàng)建包含返回棧的pendingIntent
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());
上述操作后筷登,從通知啟動(dòng)ResultActivity,按返回鍵會(huì)回到MainActivity哩盲,而不是主屏幕前方。
### 特殊Activity
> 默認(rèn)情況下狈醉,從通知啟動(dòng)的Activity會(huì)在近期任務(wù)列表里出現(xiàn)。如果不需要在近期任務(wù)里顯示惠险,則需要做以下操作:
1苗傅、在manifest中定義Activity
<activity
android:name=".ResultActivity"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true">
</activity>
2、構(gòu)建PendingIntent
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent notifyIntent = new Intent(this, ResultActivity.class);
// Sets the Activity to start in a new, empty task
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent notifyPendingIntent =
PendingIntent.getActivity(this, 0, notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(notifyPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());
上述操作后班巩,從通知啟動(dòng)ResultActivity渣慕,此Activity不會(huì)出現(xiàn)在近期任務(wù)列表中。