我正在尝试使用firebase admin sdk创建应用程序,此管理应用程序将通过向firebase admin sdk subscription方法传递注册令牌列表来为设备订阅主题。
当我试图这样做,我得到的主题错误。有谁能告诉我为什么会出现这个错误,我应该把serviceaccountkey.json文件保存在哪里,这样它才可读。
这是我的密码
public class MainActivity extends AppCompatActivity {
String TOPIC="shop";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FileInputStream serviceAccount;
File serviceAccountFile = new File("./ServiceAccountKey.json");
Boolean isFile =serviceAccountFile.isFile();
Boolean canRead = serviceAccountFile.canRead();
if (serviceAccountFile.isFile() && serviceAccountFile.canRead()) {
{
try {
serviceAccount = new FileInputStream(serviceAccountFile);
FirebaseOptions options = FirebaseOptions.builder().setCredentials(GoogleCredentials.fromStream(serviceAccount)).build();
FirebaseApp.initializeApp(options);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Firestore db = (Firestore) FirestoreClient.getFirestore().collection("USERS").document().get();
List<String> registrationTokens = Arrays.asList(
"TOKEN1", "TOKEN1"
);
TopicManagementResponse response;
{
try {
response = FirebaseMessaging.getInstance().subscribeToTopic(
registrationTokens, TOPIC);
System.out.println(response.getSuccessCount() + " tokens were subscribed successfully");
} catch (FirebaseMessagingException e) {
e.printStackTrace();
}
}
}
} }
! 我的项目结构文件的当前位置
我的应用程序版本.gradle
apply plugin: 'com.android.application'
android{compilesdkversion 29 buildtoolsversion“30.0.0”
defaultConfig {
applicationId "com.example.firebaseadminsdk"
minSdkVersion 22
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
customDebugType {
debuggable true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
packagingOptions {
exclude 'META-INF/INDEX.LIST'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/native-image/io.netty/transport/native-image.properties'
exclude 'META-INF/native-image/io.netty/transport/reflection-config.json'
exclude 'META-INF/native-image/io.netty/codec-http/native-image.properties'
exclude 'META-INF/native-image/io.netty/buffer/native-image.properties'
exclude 'META-INF/native-image/io.netty/handler/native-image.properties'
exclude 'META-INF/native-image/io.netty/common/native-image.properties'
exclude 'META-INF/io.netty.versions.properties'
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.google.firebase:firebase-admin:7.0.1'
}
暂无答案!
目前还没有任何答案,快来回答吧!