我尝试使用Google Cloud Message向我的应用程序发送通知。
我创建了一个新项目并实现了代码,这项工作正常。但是当我在我的应用程序中尝试实现时,应用程序显示此错误消息:
错误消息:
08-07 14:48:57.735 324-1057/br.com.pkg.project E/AndroidRuntime: FATAL EXCEPTION: IntentService[]
Process: br.com.pkg.project, PID: 324
java.lang.IncompatibleClassChangeError: The method 'java.io.File android.support.v4.content.ContextCompat.getNoBackupFilesDir(android.content.Context)' was expected to be of type virtual but instead was found to be of type direct (declaration of 'com.google.android.gms.iid.zzd' appears in /data/data/br.com.auttar.auttarmobile/files/instant-run/dex/slice-com.google.android.gms-play-services-gcm-8.3.0_22bae0a13164f788de113fe2ab4647903157f661-classes.dex)
at com.google.android.gms.iid.zzd.zzdL(Unknown Source)
at com.google.android.gms.iid.zzd.<init>(Unknown Source)
at com.google.android.gms.iid.zzd.<init>(Unknown Source)
at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
at br.com.pkg.project.tools.util.gcm.GCMRegistrationIntentService.registerGCM(GCMRegistrationIntentService.java:37)
at br.com.pkg.project.tools.util.gcm.GCMRegistrationIntentService.onHandleIntent(GCMRegistrationIntentService.java:29)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
我在GCMRegistrationIntentService.class中有这个方法
private void registerGCM() {
Intent registrationComplete = null;
String token = null;
try{
InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Log.v("GCMRegIntentService","TOKEN: "+token);
registrationComplete = new Intent(REGISTRATION_SUCESS);
registrationComplete.putExtra("token",token);
} catch (Exception e) {
Log.v("GCMRegIntentService","ERRO AO REGISTRAR O TOKEN");
registrationComplete = new Intent(REGISTRATION_ERROR);
}
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
它在这一行中出现的错误:
InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
Manifest.xml权限
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<!-- uses-permission android:name="android.permission.READ_CONTACTS" / -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<permission android:name="br.com.pkg.project.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="br.com.pkg.project.permission.C2D_MESSAGE" />
Manifest.xml接收器
enter code here <receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<!-- PODE SER AQUI -->
<category android:name="br.com.pkg.project"/>
</intent-filter>
</receiver>
<service android:name=".tools.util.gcm.GCMPushReceiverService" android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
</intent-filter>
</service>
<service android:name=".tools.util.gcm.GCMRegistrationIntentService" android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
的build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "br.com.pkg.project"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/ASL2.0'
exclude 'META-INF/LICENSE'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/notice.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:+'
//compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.google.code.gson:gson:2.2.4'
//compile 'com.google.android.gms:play-services:9.4.0'
compile 'com.google.android.gms:play-services:8.3.0'
// http://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
//compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.4'
//compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
compile 'com.jjoe64:graphview:4.0.1'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
//compile 'org.springframework.android:spring-android-rest-template:2.0.0.M1'
compile 'com.fasterxml.jackson.core:jackson-databind:2.3.2'
compile files('libs/spring-android-rest-template-1.0.1.RELEASE.jar')
}
的build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.google.gms:google-services:1.5.0-beta2'
classpath 'com.android.tools.build:gradle:2.0.0-alpha6'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
我在StackOverFlow中咨询了其他类似的问题,但没有人解决我的问题。
有人可以帮助我吗?
解决
我包含了这种依赖
configurations.all {
resolutionStrategy {
//force 'com.android.support:design:23.4.0'
force 'com.android.support:support-v4:23.4.0'
force 'com.android.support:appcompat-v7:23.4.0'
}
}
我的研究,发现的内容:GCM无法使用支持版本&gt; = 24
如果我错了,请纠正我,但它有效
感谢所有