原文地址:http://blog.csdn.net/intbird
notification有个类似AlertDialog的Buidler构造类,不过低版本api要兼顾,暂时不考虑
弹出时可以根据不用类型Map的队列Queue去清除指定类型的数量的消息数量,避免弹出烦人的已大串,这里的都弹...;
Notification
public static void makeNotifyCation(Context context,
CharSequence tickText,
CharSequence title,CharSequence content,
boolean hasIntents,Intent[] intents) {
NotificationManager nManger = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notifi = new Notification();
notifi.defaults = Notification.DEFAULT_ALL;
notifi.flags = Notification.FLAG_AUTO_CANCEL;
notifi.when = System.currentTimeMillis();
notifi.icon=R.drawable.ic_launcher;
notifi.tickerText = tickText;
RemoteViews contentView= new RemoteViews(context.getPackageName(), R.layout.notification);
contentView.setImageViewResource(R.id.notification_icon, R.drawable.ic_launcher);
contentView.setTextViewText(R.id.notification_title,title);
contentView.setTextViewText(R.id.notification_name, content);
notifi.contentView=contentView;
if(hasIntents){
PendingIntent pd = PendingIntent.getActivities(context, 0,intents, Intent.FLAG_ACTIVITY_NEW_TASK);
notifi.contentIntent= pd;
}
int msgId = (int) System.currentTimeMillis();
nManger.notify(msgId, notifi);
}
public static void clearNotifyCation(Context context,
int customDefineNotifyId) {
NotificationManager nManger = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
nManger.cancel(customDefineNotifyId);
}
<pre name="code" class="java"><span style="white-space:pre"> </span>//低版本
public static void makeNotifyCation(Context context,
CharSequence tickText,
CharSequence title,CharSequence content,
Intent intent) {
NotificationManager nManger = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notifi = new Notification();
notifi.defaults = Notification.DEFAULT_ALL;
notifi.flags = Notification.FLAG_AUTO_CANCEL;
notifi.when = System.currentTimeMillis();
notifi.icon=R.drawable.ic_launcher;
notifi.tickerText = tickText;
RemoteViews contentView= new RemoteViews(context.getPackageName(), R.layout.notification);
contentView.setImageViewResource(R.id.notification_icon, R.drawable.ic_launcher);
contentView.setTextViewText(R.id.notification_title,title);
contentView.setTextViewText(R.id.notification_name, content);
notifi.contentView=contentView;
int msgId = (int) System.currentTimeMillis();
if(null!=intent){
PendingIntent pd = PendingIntent.getActivity(context, msgId,intent, Intent.FLAG_ACTIVITY_NEW_TASK);
notifi.contentIntent= pd;
}
nManger.notify(msgId, notifi);
}
控制弹出,打开一系类的Activity
public static void parseMesssage(Context context,String data){
String usrId = parseJson("usrId",data);
if(!isCanNotify(usrId)){
//should make this message not read and force to push it to somebody in next time;
return ;
}
String title= parseJson("title",data);
String content = parseJson("msg",data);
int type = Integer.valueOf(parseJson("go",data));
String params = data;
boolean hasIntent = true;
Intent[] intents = null;
if(ActivityStackManager.getInstance().activityCount()<=0){
intents = new Intent[2];
intents[0] = parseIntent(context,JsInterfaceType.GO_SPLASH,null);
intents[1] = parseIntent(context,type,params);
}else{
intents = new Intent[1];
//if(isAppIsRuning()){hasIntent = false;}//no need in needs;
intents[0]= parseIntent(context,type,params);
}
MyNotification.makeNotifyCation(context,content,title,content,hasIntent,intents);
}
@Override
<span style="white-space:pre"> </span>public void onReceive(Context context, Intent intent) {
<span style="white-space:pre"> </span>mContext = context;
<span style="white-space:pre"> </span>Bundle bundle = intent.getExtras();
<span style="white-space:pre"> </span>if(bundle==null)
<span style="white-space:pre"> </span>return ;
<span style="white-space:pre"> </span>switch (bundle.getInt(PushConsts.CMD_ACTION)) {
<span style="white-space:pre"> </span>case PushConsts.GET_MSG_DATA:
byte[] payload = bundle.getByteArray("payload");
if (payload != null){
String data = new String(payload);
JsInterfaceHelper.parseMesssage(context,data);
}