Created
October 2, 2019 00:27
-
-
Save yongjhih/6bf8a2a84314b58b298c753f4dd0f989 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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