2017-09-22 56 views
1

中生成此處應在客戶註冊時生成GCM ID。但拋出錯誤。GCM ID未在android

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

什麼是描述了'MyInstanceIDListenerService M =新MyInstanceIDListenerService(此,schoolId );'?你不知道,如何使用android服務?用清單聲明它,然後等待。令牌生成異步,並可能更改 –

+0

,並且還可以在任何地方移除該行'FirebaseApp.initializeApp(context);'。切勿手動調用它,除非您正在擴展我的應用程序類 –

+0

。它仍然沒有解決這個問題 – deva11

回答