我尝试使用Firebase Cloud Messaging,但遇到一些问题。
我写了用于订阅主题的代码,但是当我在addOnCompleteListener中使用时,出现此错误:
无法解析方法“ addOnCompleteListener(live.jtube.app.MassegeNetsActivity,匿名com.google.android.gms.tasks.OnCompleteListener)”
这是我的部分代码:
MassegeNetsActivity.java代码:
.....
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.messaging.FirebaseMessaging;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
.....
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.massege_nets);
FirebaseMessaging.getInstance().subscribeToTopic("weather")
.addOnCompleteListener(MassegeNetsActivity.this, new OnCompleteListener<Void>() { //here i got the error
@Override
public void onComplete(@NonNull Task<Void> task) {
String msg = "תהליך הרשמה לערוץ בוצע בהצלחה!";
if (!task.isSuccessful()) {
msg = "אופס! תהליך הרישום לערוץ נכשל, אנא בדוק את החיבור שלך לאינטרנט";
}
// Log.d(TAG, msg);
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
});
......
和这里build.gradle文件:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 28
buildToolsVersion "28.0.2"
defaultConfig {
applicationId "app.app.app"
minSdkVersion 15
targetSdkVersion 28
versionCode 34
versionName "4.17"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support.constraint:constraint-layout:1.1.3'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
//firebase
//firebase analytics
// compile 'com.google.firebase:firebase-messaging:12.0.1'//firebase Cloud Messaging
compile 'com.android.support:appcompat-v7:28.0.0'
compile 'com.android.support:design:28.0.0'
compile 'com.android.support:support-vector-drawable:28.0.0'
compile 'com.google.firebase:firebase-core:16.0.1'
compile 'com.google.firebase:firebase-messaging:12.0.1'
compile 'com.google.firebase:firebase-ads:12.0.1'
compile 'com.google.firebase:firebase-analytics:15.0.0'
compile 'com.google.firebase:firebase-auth:15.0.0'
//implementation 'com.google.firebase:firebase-analytics:12.0.1'
compile 'com.android.support:support-v4:28.0.0'
compile "com.android.support:support-compat:28.0.0"
compile 'com.android.support:support-media-compat:28.0.0'
testCompile 'junit:junit:4.12'
}
我在Google上搜索了将近3个小时,但找不到有效的答案...
你能帮助我吗?
非常感谢你!
最佳答案
您在注释中链接的示例仅传递OnCompleteListener
,而代码之前附加了一个MassegeNetsActivity.this
作为参数。尽管有一个带有两个参数的addOnCompleteListener
,但在重载中,第一个参数是Executor
,看来Activity
实例不是有效的Executor
对象。
解决方案是仅使用侦听器调用addOnCompleteListener
:
FirebaseMessaging.getInstance().subscribeToTopic("weather")
.addOnCompleteListener(new OnCompleteListener<Void>() {
...