Skip to content

Instantly share code, notes, and snippets.

@yongjhih
Created October 2, 2019 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yongjhih/6bf8a2a84314b58b298c753f4dd0f989 to your computer and use it in GitHub Desktop.
Save yongjhih/6bf8a2a84314b58b298c753f4dd0f989 to your computer and use it in GitHub Desktop.
fun init() {
val awsConfig: AWSConfiguration = AWSConfiguration(this)
AWSMobileClient.getInstance().initializes(this, awsConfig) {
onResult {
Log.d("MainActivity", "${it.userState}")
}
onError { e ->
e.printStackTrace()
}
}
val pinpoint = PinpointManager(PinpointConfiguration(
this,
AWSMobileClient.getInstance(),
awsConfig))
FirebaseInstanceId.getInstance().instanceId.addOnCompleteListener { task ->
if (task.isSuccessful()) {
val token = task?.result?.token
Log.d("MainActivity", "Registering push notifications token: ${token}")
token?.let { pinpoint.getNotificationClient().registerDeviceToken(token) }
} else {
Log.w("MainActivity", "getInstanceId failed", task.getException())
}
}
fun AWSMobileClient.initializes(context: Context, config: AWSConfiguration, init: Callbacks<UserStateDetails>.() -> Unit) : AWSCredentialsProvider {
val callbacks = Callbacks<UserStateDetails>()
callbacks.init()
this.initialize(context, config, callbacks)
return this
}
class Callbacks<T> : com.amazonaws.mobile.client.Callback<T> {
var onResultFunc: (T) -> Unit = {}
var onErrorFunc: (Throwable) -> Unit = {}
override fun onResult(result: T) {
onResultFunc(result)
}
fun onResult(onResult: (T) -> Unit) {
this.onResultFunc = onResult
}
override fun onError(e: Exception?) {
onErrorFunc(e ?: Exception())
}
fun onError(onError: (Throwable) -> Unit) {
this.onErrorFunc = onError
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment