Android O使用通知遇到的坑

时间:2024-04-14 08:04:19

今天在Android 8.0上使用通知的时候,遇到一个问题,只要一发出通知,就弹出系统界面已停止运行的弹框。如下图所示

Android O使用通知遇到的坑


代码如下:

NotificationChannel chan1 = new NotificationChannel(PRIMARY_CHANNEL,
        "cd", NotificationManager.IMPORTANCE_DEFAULT);
chan1.setLightColor(Color.GREEN);
chan1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notificationManager.createNotificationChannel(chan1);


builder = new Notification.Builder(getApplicationContext(), PRIMARY_CHANNEL)
        .setContentTitle("ds")
        .setContentText("fa")
        .setSmallIcon(R.mipmap.ic_launcher)
        .setAutoCancel(true);
notificationManager.notify(NOTI_ID, builder.build());

Log显示如下:

I/DynamiteModule: Considering local module com.google.android.gms.maps_dynamite:0 and remote module com.google.android.gms.maps_dynamite:19
I/DynamiteModule: Selected remote version of com.google.android.gms.maps_dynamite, version >= 19
I/Google Maps Android API: Google Play services client version: 11400000
I/Google Maps Android API: Google Play services package version: 11745036
I/zygote64: Do full code cache collection, code=124KB, data=85KB
I/zygote64: After code cache collection, code=122KB, data=70KB
I/zygote64: Do partial code cache collection, code=124KB, data=73KB
I/zygote64: After code cache collection, code=124KB, data=73KB
I/zygote64: Increasing code cache capacity to 512KB
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/Choreographer: Skipped 77 frames!  The application may be doing too much work on its main thread.
W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found.
I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:4
I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 4
W/zygote64: Verification of void j.<clinit>() took 305.110ms


刚开始根据log在google上搜了半天,没半点眉目,之后很无奈的一行一行的注释掉代码排除问题所在。结果发现

builder = new Notification.Builder(getApplicationContext(), PRIMARY_CHANNEL)
        .setContentTitle("ds")
        .setContentText("fa")
        .setSmallIcon(R.mipmap.ic_launcher)
        .setAutoCancel(true);
把上面红色部分注释掉,正常,取消注释就崩掉,那么这就是问题所在了。把图片复制到drawable-xx文件夹下,
setSmallIcon(R.mipmap.ic_launcher)改为 setSmallIcon(R.drawable.ic_launcher)就正常了。虽然问题解决了,但是依然很困惑。drawable和
mipmap在使用上应该是一样的,只是mipmap在性能上做了优化。希望有知道的能解释一下