同时显示多个 Notification

主要出在PendingIntent.getActivity();的第二个参数,API文档里虽说是未被使用的参数(给出的例子也直接写0的),其实是经过该参数来区别不一样的Intent的,若是id相同,就会覆盖掉以前的Intent了。因此老是获取到最后一个Intent。java

 

只要每一个不一样的Intent对应传递一个独立的ID就能够了,以上函数修改以下(增长ID参数):android


 

package com.gf.messaging.implemention.handler;

import org.json.JSONObject;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.gf.messaging.MessagePushService;
import com.gf.messaging.MessagePushServiceConfig;

public class NotificationHandler {
	private static int NOTIFICATION_ID = 0x30001;//通知栏消息id
	private MessagePushService mService;
	private MessagePushServiceConfig mConfig;
	
	public NotificationHandler(MessagePushService service, MessagePushServiceConfig config){
		mService = service;
		mConfig = config;
	}
	
	public void handleNotification(JSONObject jsonPayload) {
		PushNotiInfo pni = ExplanationPushNoti.getPushNoti(jsonPayload);
		String payload = jsonPayload.optJSONObject("data").optString("message");
		NotificationManager notificationManager = (NotificationManager) mService.getSystemService(Context.NOTIFICATION_SERVICE);
		

		Notification notification = new Notification(
				mConfig.iconId, payload, System
						.currentTimeMillis());
		notification.flags |= Notification.FLAG_AUTO_CANCEL;

		Intent launchIntent = new Intent("com.gf.messaging.QuotationWindow");//mConfig.intentAction
		launchIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); 
		launchIntent.putExtra("stock_code",
				pni.code);
		launchIntent.putExtra("stock_name",
				pni.stockName);
		String temp = pni.market;
		if(temp.equals("sz"))
		launchIntent.putExtra("stock_market",
				1);
		else
			launchIntent.putExtra("stock_market",
					0);
		
		PendingIntent pendingIntent = PendingIntent.getActivity(mService.getApplicationContext(), 
				NOTIFICATION_ID, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		notification.contentIntent = pendingIntent;
		notification.setLatestEventInfo(
				mService.getApplicationContext(),
				mConfig.notificationTitle, payload, pendingIntent);

		notificationManager.notify(NOTIFICATION_ID++,
				notification);
		
		if(NOTIFICATION_ID == 10) notificationManager.cancel(NOTIFICATION_ID - 10);// 取消以前的通知消息;

	}
}

 

 

更多的移动互联网的发展趋势app开发移动互联网应用相关的资料请到互联网的一点事www.yidin.net 留言json

android QQ群:222392467app

资料:函数

http://www.yidin.net/?p=8280spa

http://www.yidin.net/?p=9725.net

http://my.oschina.net/yidinshi/blog/133729
code

相关文章
相关标签/搜索