|
| 1 | +/* |
| 2 | + * Copyright 2021, Leanplum, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + * or more contributor license agreements. See the NOTICE file |
| 6 | + * distributed with this work for additional information |
| 7 | + * regarding copyright ownership. The ASF licenses this file |
| 8 | + * to you under the Apache License, Version 2.0 (the |
| 9 | + * "License"); you may not use this file except in compliance |
| 10 | + * with the License. You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, |
| 15 | + * software distributed under the License is distributed on an |
| 16 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | + * KIND, either express or implied. See the License for the |
| 18 | + * specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + */ |
| 21 | + |
| 22 | +package com.leanplum |
| 23 | + |
| 24 | +import android.text.TextUtils |
| 25 | +import com.google.firebase.iid.FirebaseInstanceId |
| 26 | +import com.google.firebase.messaging.FirebaseMessaging |
| 27 | +import com.leanplum.internal.Log |
| 28 | + |
| 29 | +internal fun updateRegistrationId(provider: LeanplumCloudMessagingProvider) { |
| 30 | + try { |
| 31 | + Present.updateRegistrationId(provider) |
| 32 | + } catch (e: NoSuchMethodError) { |
| 33 | + Log.d("Using legacy firebase methods.") |
| 34 | + Legacy.updateRegistrationId(provider) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +internal fun unregister() { |
| 39 | + try { |
| 40 | + Present.unregister() |
| 41 | + } catch (e: NoSuchMethodError) { |
| 42 | + Log.d("Using legacy firebase methods.") |
| 43 | + Legacy.unregister() |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Present Firebase interface was added in version 20.3.0. |
| 49 | + */ |
| 50 | +private object Present { |
| 51 | + fun updateRegistrationId(provider: LeanplumCloudMessagingProvider) { |
| 52 | + FirebaseMessaging.getInstance().token.addOnCompleteListener { |
| 53 | + if (it.isSuccessful) { |
| 54 | + val token = it.result.toString() |
| 55 | + if (!TextUtils.isEmpty(token)) { |
| 56 | + provider.registrationId = token |
| 57 | + } |
| 58 | + } else { |
| 59 | + Log.e("getToken failed:\n" + Log.getStackTraceString(it.exception)) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + fun unregister() { |
| 65 | + try { |
| 66 | + FirebaseMessaging.getInstance().deleteToken() |
| 67 | + Log.i("Application was unregistered from FirebaseMessaging.") |
| 68 | + } catch (e: Exception) { |
| 69 | + Log.e("Failed to unregister from FirebaseMessaging.") |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Legacy Firebase interface was removed in version 22.0.0. |
| 76 | + */ |
| 77 | +private object Legacy { |
| 78 | + fun updateRegistrationId(provider: LeanplumCloudMessagingProvider) { |
| 79 | + FirebaseInstanceId.getInstance().instanceId.addOnCompleteListener { |
| 80 | + if (it.isSuccessful) { |
| 81 | + val tokenId = it.result?.token |
| 82 | + if (!TextUtils.isEmpty(tokenId)) { |
| 83 | + provider.registrationId = tokenId |
| 84 | + } |
| 85 | + } else { |
| 86 | + Log.e("getInstanceId failed:\n" + Log.getStackTraceString(it.exception)) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + fun unregister() { |
| 92 | + try { |
| 93 | + FirebaseInstanceId.getInstance().deleteInstanceId() |
| 94 | + Log.i("Application was unregistered from FCM.") |
| 95 | + } catch (e: Exception) { |
| 96 | + Log.e("Failed to unregister from FCM.") |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments