android 接受消息广播,简单的自定义notification样式弹出

原文地址: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);
            }








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值