推送通知时获得致命异常

时间:2018-04-04 15:49:24

标签: java android unity3d gradle

当我使用适用于Unity的Android插件推送onPostExecute功能的通知时,我收到此错误。

04-04 15:30:39.876 8765-8765/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.akgames.kazzan, PID: 8765
java.lang.NoSuchMethodError: No direct method <init>(Landroid/content/Context;Ljava/lang/String;)V in class Landroid/support/v4/app/NotificationCompat$Builder; or its super classes (declaration of 'android.support.v4.app.NotificationCompat$Builder' appears in /data/app/com.akgames.kazzan-2/base.apk)
at com.alptugrul.background.Baslangic.bildirimGonderAnam(Baslangic.java:222)
at com.alptugrul.background.GetData.onPostExecute(GetData.java:100)
at com.alptugrul.background.GetData.onPostExecute(GetData.java:27)
at android.os.AsyncTask.finish(AsyncTask.java:660)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

这是我的Gradle文件:

apply plugin: 'com.android.library'

allprojects {
    repositories {
        jcenter()
        google()
    }
}

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.3"

    defaultConfig {
       minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:26.1.0'
      androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'
    compile files('libs/classes.jar')
}

通知脚本:( bildirimGonderAnam是我发送通知的函数)

private static Set<String> channels = new HashSet<>();

    static void bildirimGonderAnam (Context contextsimdi,String title,String message,String ticker,String s_icon,String l_icon,int color,String bundle,
    Boolean sound,String soundName,Boolean vibrate,Boolean lights,int id,String channel) {


        NotificationManager notificationManager = (NotificationManager)contextsimdi.getSystemService(Context.NOTIFICATION_SERVICE);





        Resources res = contextsimdi.getResources();


        Intent notificationIntent = contextsimdi.getPackageManager().getLaunchIntentForPackage(bundle);
        PendingIntent pendingIntent = PendingIntent.getActivity(contextsimdi, 0,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);



        if (channel == null)
            channel = "default";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannelIfNeeded(channel, title, soundName, lights, vibrate, bundle);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(contextsimdi, channel);

        builder.setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .setContentTitle(title)
                .setContentText(message);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            builder.setColor(color);

        if (ticker != null && ticker.length() > 0)
            builder.setTicker(ticker);


        builder.setChannelId("Kazzan");

        if (s_icon != null && s_icon.length() > 0)

            builder.setSmallIcon(res.getIdentifier(s_icon, "drawable", contextsimdi.getPackageName()));

        if (l_icon != null && l_icon.length() > 0)
            builder.setLargeIcon(BitmapFactory.decodeResource(res, res.getIdentifier(l_icon, "drawable", contextsimdi.getPackageName())));

        if (sound) {
            if (soundName != null) {
                int identifier = res.getIdentifier("raw/" + soundName, null, contextsimdi.getPackageName());
                builder.setSound(Uri.parse("android.resource://" + bundle + "/" + identifier));
            } else
                builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
        }
        if (vibrate)
            builder.setVibrate(new long[] {
                    1000L, 1000L
            });

        if (lights)
            builder.setLights(Color.GREEN, 3000, 3000);

        Notification notification = builder.build();
        notificationManager.notify(id, notification);
    }



    @TargetApi(24)
    public static void CreateChannel(String identifier, String name, String description, int importance, String soundName, int enableLights, int lightColor, int enableVibration, long[] vibrationPattern, String bundle) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
            return;

        channels.add(identifier);

        NotificationManager nm = (NotificationManager) UnityPlayer.currentActivity.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel = new NotificationChannel(identifier, name, importance);
        channel.setDescription(description);
        if (soundName != null) {
            Resources res = UnityPlayer.currentActivity.getResources();
            int id = res.getIdentifier("raw/" + soundName, null, UnityPlayer.currentActivity.getPackageName());
            AudioAttributes audioAttributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build();
            channel.setSound(Uri.parse("android.resource://" + bundle + "/" + id), audioAttributes);
        }

        channel.enableLights(enableLights == 1);
        channel.setLightColor(lightColor);
        channel.enableVibration(enableVibration == 1);
        if (vibrationPattern == null)
            vibrationPattern = new long[] { 1000L, 1000L };
        channel.setVibrationPattern(vibrationPattern);
        nm.createNotificationChannel(channel);
    }

    @TargetApi(24)
    private static void createChannelIfNeeded(String identifier, String name, String soundName, boolean enableLights, boolean enableVibration, String bundle) {
        if (channels.contains(identifier))
            return;
        channels.add(identifier);

        CreateChannel(identifier, name, identifier + " notifications", NotificationManager.IMPORTANCE_HIGH, soundName, enableLights ? 1 : 0, Color.GREEN, enableVibration ? 1 : 0, null, bundle);
    }

我在第28行收到错误。

所以,我在我的项目中也使用Facebook插件,而Facebook插件在Plugins \ Android文件夹中使用com.android.support.appcompat-v7-25.3.1。我怎样才能解决我的问题?感谢。

1 个答案:

答案 0 :(得分:1)

看起来像依赖性问题。

尝试在您的应用级草图文件中使用此代码:

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '26.1.0'
            }
        }
    }
}

或者

allprojects {
    repositories {
       //...
    }

    subprojects {
        project.configurations.all {
            resolutionStrategy.eachDependency { details ->
                if (details.requested.group == 'com.android.support'
                && !details.requested.name.contains('multidex') ) {
                    details.useVersion "26.1.0"
                }
            }
        }
    }
}
  

注意:顺便说一句,我喜欢您的方法bildirimGonderAnam的名称,   这很有趣(土耳其语)。