android4.4以上notification源码分析

客户端发送消息

Notification.Builder builder = new Notification.Builder(context);
        builder.setSmallIcon(R.drawable.ic_noti_usb);
        Notification notification = builder
                .setContentTitle("标题")
                .setContentText("notification内容")
                .build();
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(00001, notification);

 1.NotificationManager.java

public void notify(int id, Notification notification) {
	notify(null, id, notification);
}
public void notify(String tag, int id, Notification notification) {
	notifyAsUser(tag, id, notification, new UserHandle(UserHandle.myUserId()));
}
/**
 * @hide
 */
public void notifyAsUser(String tag, int id, Notification notification, UserHandle user)
{
	INotificationManager service = getService();
	String pkg = mContext.getPackageName();
	// Fix the notification as best we can.
	Notification.addFieldsFromContext(mContext, notification);
	if (notification.sound != null) {
		notification.sound = notification.sound.getCanonicalUri();
		if (StrictMode.vmFileUriExposureEnabled()) {
			notification.sound.checkFileUriExposed("Notification.sound");
		}
	}
	fixLegacySmallIcon(notification, pkg);
	if (mContext.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
		if (notification.getSmallIcon() == null) {
			throw new IllegalArgumentException("Invalid notification (no valid small icon): "
					+ notification);
		}
	}
	if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
	final Notification copy = Builder.maybeCloneStrippedForDelivery(notification);
	try {
		service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id,
				copy, user.getIdentifier());
	} catch (RemoteException e) {
		throw e.rethrowFromSystemServer();
	}
}

 

if (mContext.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
	if (notification.getSmallIcon() == null) {
		throw new IllegalArgumentException("Invalid notification (no valid small icon): "
				+ notification);
	}
}

创造消息时一定要setSmallIcon,否则走到这就报异常了

notifyAsUser中service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id,copy, user.getIdentifier())会调用到NotificationManagerService类中

2.NotificationManagerService.java

该类源码位置

android4.4版本: frameworks\base\services\java\com\android\server\NotificationManagerService.java

android4.4以上: frameworks\base\services\core\java\com\android\server\notification\NotificationManagerService.java

上述的INotificationManager service.enqueueNotificationWithTag会调用到NotificationManagerService类中的INotificationManager下的enqueueNotificationWithTag

private final IBinder mServi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值