Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. fun init() {
  2. val awsConfig: AWSConfiguration = AWSConfiguration(this)
  3.  
  4. AWSMobileClient.getInstance().initializes(this, awsConfig) {
  5. onResult {
  6. Log.d("MainActivity", "${it.userState}")
  7. }
  8. onError { e ->
  9. e.printStackTrace()
  10. }
  11. }
  12. val pinpoint = PinpointManager(PinpointConfiguration(
  13. this,
  14. AWSMobileClient.getInstance(),
  15. awsConfig))
  16.  
  17.  
  18. FirebaseInstanceId.getInstance().instanceId.addOnCompleteListener { task ->
  19. if (task.isSuccessful()) {
  20. val token = task?.result?.token
  21. Log.d("MainActivity", "Registering push notifications token: ${token}")
  22. token?.let { pinpoint.getNotificationClient().registerDeviceToken(token) }
  23. } else {
  24. Log.w("MainActivity", "getInstanceId failed", task.getException())
  25. }
  26. }
  27.  
  28. fun AWSMobileClient.initializes(context: Context, config: AWSConfiguration, init: Callbacks<UserStateDetails>.() -> Unit) : AWSCredentialsProvider {
  29. val callbacks = Callbacks<UserStateDetails>()
  30. callbacks.init()
  31. this.initialize(context, config, callbacks)
  32. return this
  33. }
  34.  
  35. class Callbacks<T> : com.amazonaws.mobile.client.Callback<T> {
  36. var onResultFunc: (T) -> Unit = {}
  37. var onErrorFunc: (Throwable) -> Unit = {}
  38.  
  39. override fun onResult(result: T) {
  40. onResultFunc(result)
  41. }
  42.  
  43. fun onResult(onResult: (T) -> Unit) {
  44. this.onResultFunc = onResult
  45. }
  46.  
  47. override fun onError(e: Exception?) {
  48. onErrorFunc(e ?: Exception())
  49. }
  50.  
  51. fun onError(onError: (Throwable) -> Unit) {
  52. this.onErrorFunc = onError
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement