首先創(chuàng)建一個(gè)通知欄,本例子子寫出了部分常用的屬性尺锚,其他的屬性自行百度珠闰, json是服務(wù)器返回的參數(shù)
public void showNotifictionIcon(Context context) {
String title = json.optString("title");
String content = json.optString("content");
NotificationManager manager = (NotificationManager) mContext.getSystemService(mContext.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
builder.setTicker("來自客滿滿的通知");//通知首次出現(xiàn)在通知欄,帶上升動(dòng)畫效果的
builder.setAutoCancel(true);//點(diǎn)擊后消失
builder.setSmallIcon(R.drawable.loge);//設(shè)置通知欄消息標(biāo)題的頭像
builder.setDefaults(NotificationCompat.DEFAULT_SOUND);//設(shè)置通知鈴聲
builder.setContentTitle(title);//設(shè)置標(biāo)題
builder.setContentText(content);//設(shè)置內(nèi)容
builder.setPriority(Notification.PRIORITY_DEFAULT); //設(shè)置該通知優(yōu)先級(jí)
//利用PendingIntent來包裝我們的intent對(duì)象,使其延遲跳轉(zhuǎn) 設(shè)置通知欄點(diǎn)擊意圖
builder.setContentIntent(createIntent(json));
// builder.setDeleteIntent(createDeleteIntent(json));
manager.notify(0, builder.build());
}
通知欄創(chuàng)建的時(shí)候我們給他設(shè)定一個(gè)intent瘫辩,這樣通知欄便可以點(diǎn)擊
builder.setContentIntent(createIntent(json));
在這里科普下怎么獲取PendingIntent
你可以通過getActivity(Context context, int requestCode, Intent intent, int flags)系列方法從系統(tǒng)取得一個(gè)用于啟動(dòng)一個(gè)Activity的PendingIntent對(duì)象
可以通過getService(Context context, int requestCode, Intent intent, int flags)方法從系統(tǒng)取得一個(gè)用于啟動(dòng)一個(gè)Service的PendingIntent對(duì)象
可以通過getBroadcast(Context context, int requestCode, Intent intent, int flags)方法從系統(tǒng)取得一個(gè)用于向BroadcastReceiver的發(fā)送廣播的PendingIntent對(duì)象
PendingIntent幾個(gè)常量:
1.FLAG_CANCEL_CURRENT :如果AlarmManager管理的PendingIntent已經(jīng)存在伏嗜,那么將會(huì)取消當(dāng)前的PendingIntent,從而創(chuàng)建一個(gè)新的PendingIntent
2.FLAG_UPDATE_CURRENT:如果AlarmManager管理的PendingIntent已經(jīng)存在伐厌,可以讓新的Intent更新之前PendingIntent中的Intent對(duì)象數(shù)據(jù)承绸,例如更新Intent中的Extras,另外挣轨,我們也可以在PendingIntent的原進(jìn)程中調(diào)用PendingIntent的cancel ()把其從系統(tǒng)中移除掉
3.FLAG_NO_CREATE :如果AlarmManager管理的PendingIntent已經(jīng)存在军熏,那么將不進(jìn)行任何操作,直接返回已經(jīng)存在的PendingIntent卷扮,如果PendingIntent不存在了荡澎,那么返回null
本人通過getBroadcast(Context context, int requestCode, Intent intent, int flags)方法從系統(tǒng)取得一個(gè)用于向BroadcastReceiver的發(fā)送廣播的PendingIntent對(duì)象
創(chuàng)建點(diǎn)擊事件的廣播intent
public PendingIntent createIntent(JSONObject json) {
Intent intent;
PendingIntent contentIntent = null;
Bundle mBundle = new Bundle();
intent = new Intent();
String content = json.optString("content");
mBundle.putString("content", content);
intent.putExtras(mBundle);
intent.setClass(mContext, NotifyClickReceiver.class);
intent.setAction("com.dianping.kmm.receiver.click.notify");
contentIntent = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
return contentIntent;
}
點(diǎn)擊通知欄接收點(diǎn)擊事件的廣播均践,在onReceive里面可行性寫跳轉(zhuǎn)邏輯,千萬不要忘記廣播需要在配置文件里面注冊(cè)
public class NotifyClickReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String content = intent.getStringExtra("content");
int type = intent.getIntExtra("type", -1);
switch (type) {
case AppConstant.NOTIFY_MSG_TYPE_TRADE:
Toast.makeText(context, content + "" + type, Toast.LENGTH_LONG).show();
break;
case AppConstant.NOTIFY_MSG_TYPE_QA:
Toast.makeText(context, content + "" + type, Toast.LENGTH_LONG).show();
break;
}
}
}