Skip to content

Commit e64a1db

Browse files
committed
Add messaging service to example
1 parent 16fa50a commit e64a1db

File tree

3 files changed

+74
-13
lines changed

3 files changed

+74
-13
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package {{ sdk.namespace | caseDot }}.android.services
2+
3+
import com.google.firebase.messaging.FirebaseMessagingService
4+
import {{ sdk.namespace | caseDot }}.ID
5+
import {{ sdk.namespace | caseDot }}.services.Account
6+
import kotlinx.coroutines.runBlocking
7+
8+
class MessagingService : FirebaseMessagingService() {
9+
10+
companion object {
11+
var account: Account? = null
12+
}
13+
14+
override fun onNewToken(token: String) {
15+
super.onNewToken(token)
16+
17+
val prefs = getSharedPreferences("example", MODE_PRIVATE)
18+
19+
prefs.edit().putString("fcmToken", token).apply()
20+
21+
if (account == null) {
22+
return
23+
}
24+
25+
val targetId = prefs.getString("targetId", null)
26+
27+
runBlocking {
28+
if (targetId == null) {
29+
val target = account!!.createPushTarget(ID.unique(), token)
30+
31+
prefs.edit().putString("targetId", target.id).apply()
32+
} else {
33+
account!!.updatePushTarget(targetId, token)
34+
}
35+
}
36+
}
37+
}

templates/android/example/src/main/java/io/appwrite/android/ui/accounts/AccountsFragment.kt.twig renamed to templates/android/example/src/main/java/io/package/android/ui/accounts/AccountsFragment.kt.twig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ class AccountsFragment : Fragment() {
7070
}
7171
}
7272

73+
viewModel.target.observe(viewLifecycleOwner) { event ->
74+
event?.getContentIfNotHandled()?.let {
75+
context
76+
?.getSharedPreferences("example", Context.MODE_PRIVATE)
77+
?.edit()
78+
?.putString("targetId", it.id)
79+
?.apply()
80+
}
81+
}
82+
7383
return binding.root
7484
}
7585
}

templates/android/example/src/main/java/io/appwrite/android/ui/accounts/AccountsViewModel.kt.twig renamed to templates/android/example/src/main/java/io/package/android/ui/accounts/AccountsViewModel.kt.twig

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,43 @@ import kotlinx.coroutines.launch
1515

1616
class AccountsViewModel : ViewModel() {
1717

18-
private val _error = MutableLiveData<Event<Exception>>().apply {
19-
value = null
20-
}
18+
private val _error = MutableLiveData<Event<Exception>>().apply { value = null }
2119
val error: LiveData<Event<Exception>> = _error
2220

23-
private val _response = MutableLiveData<Event<String>>().apply {
24-
value = null
25-
}
21+
private val _response = MutableLiveData<Event<String>>().apply { value = null }
2622
val response: LiveData<Event<String>> = _response
2723

28-
private val accountService by lazy {
29-
Account(client)
24+
private val _target = MutableLiveData<Event<Target>>().apply { value = null }
25+
val target: LiveData<Event<Target>> = _target
26+
27+
private val account by lazy {
28+
val account = Account(client)
29+
30+
MessagingService.account = account
31+
32+
account
3033
}
3134

32-
fun onLogin(email: Editable, password: Editable) {
35+
fun onLogin(
36+
email: String,
37+
password: String,
38+
token: String?,
39+
) {
3340
viewModelScope.launch {
3441
try {
35-
val session = accountService.createEmailPasswordSession(
36-
email.toString(),
37-
password.toString()
42+
val session = account.createEmailPasswordSession(
43+
email,
44+
password
3845
)
46+
47+
if (token != null) {
48+
val target = account.createPushTarget(ID.unique(), token)
49+
50+
_target.postValue(Event(target))
51+
}
52+
3953
_response.postValue(Event(session.toJson()))
40-
} catch (e: {{ spec.title | caseUcfirst }}Exception) {
54+
} catch (e: AppwriteException) {
4155
_error.postValue(Event(e))
4256
}
4357
}

0 commit comments

Comments
 (0)