此处,应在客户注册时生成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.
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:1)
我花了很多时间来做这件事。
apply plugin: 'com.google.gms.google-services'
Gradle(app)中缺少此行,并且应该在依赖项之后添加到底部..它在
中描述https://developers.google.com/cloud-messaging/android/client