客户端发送消息
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