同時顯示多個 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

相關文章
相關標籤/搜索