Notification.setSound() 不适用于 proguard

Posted

技术标签:

【中文标题】Notification.setSound() 不适用于 proguard【英文标题】:Notification.setSound() isn't working with proguard 【发布时间】:2019-07-11 12:45:03 【问题描述】:

我遇到了包括通知库在内的 proguard 问题。如果启用了 proguard,我将听不到通知声音。我找不到哪个库有问题。

app/build.gradle:

实现 'com.google.android.gms:play-services-auth:16.0.1'

实现'com.google.android.gms:play-services-gcm:16.0.0'

实现 'com.google.firebase:firebase-core:16.0.7'

实现 'com.google.firebase:firebase-messaging:17.3.4'

实现'com.crashlytics.sdk.android:crashlytics:2.9.8'

实现'com.android.support:appcompat-v7:28.0.0'

实现 'com.android.support:support-v13:28.0.0'

实现 'com.android.support:design:28.0.0'

实现 'com.android.support:recyclerview-v7:28.0.0'

实现'com.android.support:cardview-v7:28.0.0'

我的保镖:

# google gms
-dontwarn com.google.android.gms.**
-keep class com.google.android.gms.**  *; 
-keep class * extends java.util.ListResourceBundle 
    protected Object[][] getContents(); 
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable 
    public static final *** NULL; 

-keep class com.google.android.gms.internal.**  *; 
-keepnames class * implements android.os.Parcelable 
    public static final ** CREATOR; 

# fabric https://docs.fabric.io/android/crashlytics/dex-and-proguard.html
-keepattributes Signature
-keep class com.crashlytics.**  *; 
-dontwarn com.crashlytics.**
-keepattributes SourceFile,LineNumberTable
-keep public class * extends java.lang.Exception

# android support
-keepnames class !android.support.v7.internal.view.menu.**, **  *; 
-keep public class android.support.v7.widget.**  *; 
-keep public class android.support.v7.internal.widget.**  *; 
-keep public class * extends android.support.v4.view.ActionProvider 
    public <init>(android.content.Context); 
-keep public class * extends android.support.design.widget.CoordinatorLayout.Behavior  *; 
-keep public class * extends android.support.design.widget.ViewOffsetBehavior  *; 

代码:

    Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification);
    AudioAttributes attributes = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                   .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                    .build();

    // over android 8.0
    NotificationChannel mChannel = new NotificationChannel(Constants.MISSED_CALL, getString(R.string.notification), NotificationManager.IMPORTANCE_HIGH);

    mChannel.setSound(uri, attributes);
    getNotificationManager().createNotificationChannel(mChannel);

    // < 8.0
    android.app.Notification notification = builder
                    .setCategory(NotificationCompat.CATEGORY_CALL)
                    .setSound(uri)
                    .setStyle(setNotificationList(notificationList))
                    .build();

【问题讨论】:

【参考方案1】:

也许配置proguard来保留通知相关的类和音频类?这是一个部分示例:

https://github.com/pusher/push-notifications-android/blob/master/pushnotifications/proguard-rules.pro

【讨论】:

谢谢,但它不起作用:(我已添加-keep class android.support.v4.app.NotificationCompat *; -keep class android.support.v4.app.NotificationCompat$* *; -keep class android.support.v4.app.NotificationManagerCompat *; 请帮帮我,我在任何地方都找不到

以上是关于Notification.setSound() 不适用于 proguard的主要内容,如果未能解决你的问题,请参考以下文章

时光不老我们不散这句话是啥意思 时光不老我们不散这句话解释

C语言 不允许使用不完整的类型

时光不老,我们不散的下一句是啥?

C语言 不允许使用不完整的类型

时光不老,我们不散珍惜友情的说说

时光不老我们不散,天不会不蓝,我不会不在。