gpt4 book ai didi

java - GCM Id 未在 android 中生成

转载 作者:行者123 更新时间:2023-11-30 00:20:37 27 4
gpt4 key购买 nike

此处,应在客户注册时生成 GCM ID。但抛出错误。

public void sendGCMKey() {
try {
FirebaseApp.initializeApp(this);

MyInstanceIDListenerService m = new MyInstanceIDListenerService(this, schoolId);
m.onTokenRefresh();
SharedPreferenceSingleton.getInstance().writeStringPreference("isFCMTokenGenerated","YES");
} catch (Exception e) {
e.printStackTrace();
}
}

这是服务类代码

 @Override
public void onTokenRefresh() {
// Get updated InstanceID token.
FirebaseApp.initializeApp(context); // Newly added 20/9/2017
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(refreshedToken);
}

private void sendRegistrationToServer(String refreshedToken) {
Log.e("Refreshed Token ::",refreshedToken);
try {
SharedPreferenceSingleton.getInstance().init(context);
SharedPreferenceSingleton.getInstance().writeStringPreference(ApplicationConstants.GCM_ID_KEY, refreshedToken);
int appVersion = getAppVersion(context);
SharedPreferenceSingleton.getInstance().writeIntPreference(ApplicationConstants.GCM_VERSION_KEY, appVersion);
SharedPreferenceSingleton.getInstance().init(context);
String mobileNumber = SharedPreferenceSingleton.getInstance().getStringPreference(ApplicationConstants.MOBILE_KEY);
JSONObject jsonObject = new JSONObject();
jsonObject.put(RegistrationContracts.MOBILE_KEY, mobileNumber);
jsonObject.put(RegistrationContracts.SCHOOL_ID_KEY, schoolId);
jsonObject.put(BroadcastContract.TYPE_KEY,BroadcastContract.TEACHER_TYPE_KEY_);
jsonObject.put(RegistrationContracts.GCM_KEY, refreshedToken);
AsyncWorkerEncrypted asyncGCMWorker = new AsyncWorkerEncrypted(context);
asyncGCMWorker.delegate = MyInstanceIDListenerService.this;
asyncGCMWorker.execute(ServerConnector.SEND_GCM_ID, jsonObject.toString(), RequestConstants.POST_REQUEST, RequestConstants.HEADER_YES, RequestConstants.GCM_SEND_ID);

} catch (Exception e){
e.printStackTrace();
}

这是抛出错误:

java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.xxx.cbcbc Make sure to call FirebaseApp.initializeApp(Context) first.

谁能帮我解决这个问题?

最佳答案

我为此花了很多时间。

apply plugin: 'com.google.gms.google-services'

Gradle (app) 中缺少此行,应在依赖项之后添加到底部。它在

中有描述

https://developers.google.com/cloud-messaging/android/client

关于java - GCM Id 未在 android 中生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46363665/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com