<lambda>null1 package host.exp.exponent.fcm
2 
3 import android.content.Context
4 import android.util.Log
5 import com.google.firebase.messaging.FirebaseMessaging
6 import host.exp.exponent.notifications.ExponentNotificationIntentService
7 import host.exp.exponent.storage.ExponentSharedPreferences
8 import java.io.IOException
9 
10 class FcmRegistrationIntentService : ExponentNotificationIntentService(TAG) {
11   var mToken: String? = null
12 
13   @Throws(IOException::class)
14   override fun getToken(): String {
15     if (mToken == null) {
16       throw IOException("No FCM token found")
17     }
18     Log.d("FCM Device Token", mToken!!)
19     return mToken!!
20   }
21 
22   override fun getSharedPrefsKey(): ExponentSharedPreferences.ExponentSharedPreferencesKey {
23     return ExponentSharedPreferences.ExponentSharedPreferencesKey.FCM_TOKEN_KEY
24   }
25 
26   override fun getServerType(): String {
27     return "fcm"
28   }
29 
30   companion object {
31     private val TAG = FcmRegistrationIntentService::class.java.simpleName
32 
33     fun getTokenAndRegister(context: Context) {
34       FirebaseMessaging.getInstance().token.addOnSuccessListener { instanceIdResult ->
35         registerForeground(
36           context,
37           instanceIdResult
38         )
39       }
40         .addOnFailureListener { e ->
41           Log.e(
42             "FCM Device Token",
43             "Error calling getToken " + e.localizedMessage
44           )
45         }
46     }
47 
48     fun registerForeground(context: Context, token: String) {
49       FcmRegistrationIntentService().apply {
50         attachBaseContext(context)
51         mToken = token
52         onHandleIntent(null)
53       }
54     }
55   }
56 }
57