同时显示多个 Notification

主要出在PendingIntent.getActivity();的第二个参数,API文档里虽然说是未被使用的参数(给出的例子也直接写0的),实际上是通过该参数来区别不同的Intent的,如果id相同,就会覆盖掉之前的Intent了。所以总是获取到最后一个Intent。

 

只要每个不同的Intent对应传递一个独立的ID就可以了,以上函数修改如下(增加ID参数):


 

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 留言

android QQ群:222392467

资料:

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

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

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

原文地址:https://www.cnblogs.com/dyllove98/p/3177842.html