dim.red

一 背景

android 的版本的更替往往需要开发者进行跟进.

Notification 在Android 8.0 上需要做一些兼容的工作.官方指导文档

二 适配

适配工作分为两步

1 根据一个channelId 生成NotificationChannel 作为推送通道

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void init(Context ctx) {
if (SDK_INT >= 26) {
String channelId = "dim";
NotificationManager manager = (NotificationManager) Application.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if (manager != null) {
NotificationChannel taco = manager.getNotificationChannel(channelId);
if (taco == null) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(channelId, channelId, importance);
channel.setDescription(TACO_CHANNEL);
manager.createNotificationChannel(channel);
}
}
}
}

2 所有的通知需要设置 channelId:

设置的方式:

1
new Notification.Builder(ctx).setChannelId(channelId);

到此兼容的工作算是完成了.
但是这里的兼容只是局限在我们自己的业务代码.

三. 业务外的代码怎么办呢?

比如说在个推的GetuiSDK2.10.3.5 并没有对 Notification 做相关的处理.

Q: 怎么办?
A: 使用 AOP 为第三方 SDK 修BUG , 适配新特性 .

AOP 的实现有很多方式,这里我们会讲3种方式来实现这个效果.

一. 使用 lancet ( 饿了么开源的 AOP 框架 )

引入:https://github.com/eleme/lancet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class NotifactionFix {

public static final String CHANNEL = "dim";

@Proxy("notify")
@TargetClass("android.app.NotificationManager")
public void notify(int id, Notification notification) {
if (SDK_INT >= 26) {
maybeSetChannelId(notification);
}
Origin.callVoid();
}

@Proxy("notify")
@TargetClass("android.app.NotificationManager")
public void notify(String tag, int id, Notification notification) {
maybeSetChannelId(notification);
Origin.callVoid();
}

public static void maybeSetChannelId(Notification notification) {
if (SDK_INT >= 26) {
if (TextUtils.isEmpty(notification.getChannelId())) {
try {
Field mChannelId = Notification.class.getDeclaredField("mChannelId");
mChannelId.setAccessible(true);
mChannelId.set(notification,CHANNEL);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}

二 使用 aspectjX

引入 :https://github.com/HujiangTechnology/gradle_plugin_android_aspectjx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@Aspect
public class NotificationFix {

public static final String CHANNEL = "dim";

private static NotificationFix sNotificationFix = new NotificationFix();

public static NotificationFix aspectOf() {
return sNotificationFix;
}

@Pointcut("call(public void android.app.NotificationManager.notify(..))")
void notificationManager_notifyMethod() {
}

@Before("notificationManager_notifyMethod()")
public void hookNotificationManager_notifyMethod(JoinPoint joinPoint) {
Log.e("dim", "hookNotificationManager_notifyMethod: ");
for (Object arg : joinPoint.getArgs()) {
if (arg != null && arg instanceof Notification) {
maybeSetChannelId((Notification) arg);
}
}
}

public static void maybeSetChannelId(Notification notification) {
if (SDK_INT >= 26) {
if (TextUtils.isEmpty(notification.getChannelId())) {
try {
Field mChannelId = Notification.class.getDeclaredField("mChannelId");
mChannelId.setAccessible(true);
mChannelId.set(notification, CHANNEL);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}

三 使用 ASM 框架.

3.1 Android Plugin 处理

在androisjd 打包中加入对 class 的处理
引入’ compile ‘org.ow2.asm:asm:6.0_ALPHA’’

1
2
3
4
5
6
7
8
9
10
byte[] process(byte[] src) {
try {
ClassReader classReader = new ClassReader(src);
ClassWriter cw = new ClassWriter(classReader, ClassWriter.COMPUTE_MAXS);
classReader.accept(new AopClassVisitor(cw, sasukeExtension), ClassReader.EXPAND_FRAMES);
return cw.toByteArray();
} catch (Exception e) {
return src;
}
}

AopClassVisitor.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class AopClassVisitor extends ClassVisitor {
private String className;

public AopClassVisitor(ClassWriter cw) {
super(Opcodes.ASM5, cw);
}

@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
className = name;
}

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
if (className.equals("red/dim/hook/NotificationFix")) {
return super.visitMethod(access, name, desc, signature, exceptions);
}
return new AopMethodVisitor(super.visitMethod(access, name, desc, signature, exceptions));
}
}

AopMethodVisitor.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class AopMethodVisitor extends MethodVisitor {
AopMethodVisitor(MethodVisitor methodVisitor) {
super(Opcodes.ASM5, methodVisitor);
}

@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
if (opcode == Opcodes.INVOKEINTERFACE
&& "android/app/NotificationManager".equals(owner)
&& "notify".equals(name)
&& "(I;Landroid/app/Notification)V".equals(desc)) {
visitMethodInsn(Opcodes.INVOKESTATIC, "red/dim/hook/NotificationFix", "notify", "(Landroid/app/NotificationManager;I;Landroid/app/Notification)V", false);
return;
}
if (opcode == Opcodes.INVOKEINTERFACE
&& "android/app/NotificationManager".equals(owner)
&& "notify".equals(name)
&& "(Ljava.lang.String;I;Landroid/app/Notification)V".equals(desc)) {
visitMethodInsn(Opcodes.INVOKESTATIC, "red/dim/hook/NotificationFix", "notify", "(Landroid/app/NotificationManager;Ljava.lang.String;I;Landroid/app/Notification)V", false);
return;
}
super.visitMethodInsn(opcode, owner, name, desc, itf);
}

}

总结

上述的代码通过字节码分析将所有的NotificationManager 的 notify 的两个方法的调用都路由到
NotificationFix 的 notify 静态方法上.

3.2 SDK 处理

在 SDK 加入处理类
red.dim.hook.NotificationFix.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class NotificationFix {

public static final String CHANNEL = "dim";

public static void notify(NotificationManager notificationManager, int id, Notification notification) {
maybeSetChannelId(notification);
notificationManager.notify(id, notification);
}

public static void notify(NotificationManager notificationManager, String tag, int id, Notification notification) {
maybeSetChannelId(notification);
notificationManager.notify(tag, id, notification);
}

private static void maybeSetChannelId(Notification notification) {
if (SDK_INT >= 26) {
if (TextUtils.isEmpty(notification.getChannelId())) {
try {
Field mChannelId = Notification.class.getDeclaredField("mChannelId");
mChannelId.setAccessible(true);
mChannelId.set(notification, CHANNEL);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}

四 总结

三种 AOP 方式 各有优势