NCFirebaseInstanceIDService.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017 Mario Danic
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.owncloud.android.services.firebase;
  21. import android.accounts.Account;
  22. import android.accounts.AuthenticatorException;
  23. import android.accounts.OperationCanceledException;
  24. import android.content.Context;
  25. import android.text.TextUtils;
  26. import com.google.firebase.iid.FirebaseInstanceId;
  27. import com.google.firebase.iid.FirebaseInstanceIdService;
  28. import com.owncloud.android.MainApp;
  29. import com.owncloud.android.R;
  30. import com.owncloud.android.authentication.AccountUtils;
  31. import com.owncloud.android.lib.common.OwnCloudAccount;
  32. import com.owncloud.android.lib.common.OwnCloudClient;
  33. import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
  34. import com.owncloud.android.lib.common.operations.RemoteOperation;
  35. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  36. import com.owncloud.android.lib.common.utils.Log_OC;
  37. import com.owncloud.android.lib.resources.notifications.RegisterAccountDeviceForNotificationsOperation;
  38. import com.owncloud.android.lib.resources.notifications.RegisterAccountDeviceForProxyOperation;
  39. import com.owncloud.android.lib.resources.notifications.models.PushResponse;
  40. import com.owncloud.android.utils.PushUtils;
  41. import java.io.IOException;
  42. import java.security.PublicKey;
  43. public class NCFirebaseInstanceIDService extends FirebaseInstanceIdService {
  44. private static final String TAG = "NCFirebaseInstanceID";
  45. @Override
  46. public void onTokenRefresh() {
  47. //You can implement this method to store the token on your server
  48. if (!TextUtils.isEmpty(getResources().getString(R.string.push_server_url))) {
  49. PushUtils.generateRsa2048KeyPair();
  50. sendRegistrationToServer(FirebaseInstanceId.getInstance().getToken());
  51. }
  52. }
  53. private void sendRegistrationToServer(String token) {
  54. String pushTokenHash = PushUtils.generateSHA512Hash(token);
  55. PublicKey devicePublicKey = (PublicKey) PushUtils.readKeyFromFile(true);
  56. if (devicePublicKey != null) {
  57. String publicKey = devicePublicKey.toString();
  58. Context context = MainApp.getAppContext();
  59. for (Account account : AccountUtils.getAccounts(context)) {
  60. try {
  61. OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
  62. OwnCloudClient mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
  63. getClientFor(ocAccount, context);
  64. RemoteOperation registerAccountDeviceForNotificationsOperation =
  65. new RegisterAccountDeviceForNotificationsOperation(pushTokenHash,
  66. publicKey, context.getResources().getString(R.string.push_server_url));
  67. RemoteOperationResult remoteOperationResult = registerAccountDeviceForNotificationsOperation.
  68. execute(mClient);
  69. if (remoteOperationResult.isSuccess()) {
  70. PushResponse pushResponse = remoteOperationResult.getPushResponseData();
  71. RemoteOperation registerAccountDeviceForProxyOperation = new
  72. RegisterAccountDeviceForProxyOperation(
  73. context.getResources().getString(R.string.push_server_url),
  74. token, pushResponse.getDeviceIdentifier(), pushResponse.getSignature(),
  75. pushResponse.getPublicKey());
  76. remoteOperationResult = registerAccountDeviceForProxyOperation.execute(mClient);
  77. }
  78. } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
  79. Log_OC.d(TAG, "Failed to find an account");
  80. } catch (AuthenticatorException e) {
  81. Log_OC.d(TAG, "Failed via AuthenticatorException");
  82. } catch (IOException e) {
  83. Log_OC.d(TAG, "Failed via IOException");
  84. } catch (OperationCanceledException e) {
  85. Log_OC.d(TAG, "Failed via OperationCanceledException");
  86. }
  87. }
  88. }
  89. }
  90. }