-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add api client with grpc kotlin * first pass at notification service code for decoding * update the pending intent * add more setup for firebase * copy over the proto code * fix package naming * add XMTPPush file to library * clean up code a bit * add the xmtp push subscribe and register * testing pushes locally * clean up the generated code for readability * correctly export the java * fix up lint * more code clean up * remove google servies json that should be added by the tester * add it to git ignore * add a readme to the push folder * the linter requires the google-services json file * add a few clean up nits * pull loading keys out into a resuable function * add required items to the gradle file but comment out so not included in library * add commented out gradle * add extra details to the read me * jha first pass of push README * update based on naomi's feedback <3 * a few more README tweaks * add link to push readme to main readme * Update README.md --------- Co-authored-by: Jennifer Hasegawa <[email protected]>
- Loading branch information
1 parent
1e26193
commit ab674a3
Showing
19 changed files
with
5,437 additions
and
18 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"project_info": { | ||
"project_number": "609788839593", | ||
"project_id": "example-notification-server", | ||
"storage_bucket": "example-notification-server.appspot.com" | ||
}, | ||
"client": [ | ||
{ | ||
"client_info": { | ||
"mobilesdk_app_id": "1:609788839593:android:0ecb48b4bdb4d0bc240bb8", | ||
"android_client_info": { | ||
"package_name": "org.xmtp.android.example" | ||
} | ||
}, | ||
"oauth_client": [ | ||
{ | ||
"client_id": "609788839593-82gepnf8nv5ml9oh34or96qqijl1si0i.apps.googleusercontent.com", | ||
"client_type": 3 | ||
} | ||
], | ||
"api_key": [ | ||
{ | ||
"current_key": "AIzaSyBnPkGEV3VYzx0VK42Q2lmhZaxufb7VIWc" | ||
}, | ||
{ | ||
"current_key": "AIzaSyA1jP-bfReDs_fGP4uJ7tFzItVS15tgFyg" | ||
} | ||
], | ||
"services": { | ||
"appinvite_service": { | ||
"other_platform_oauth_client": [ | ||
{ | ||
"client_id": "609788839593-82gepnf8nv5ml9oh34or96qqijl1si0i.apps.googleusercontent.com", | ||
"client_type": 3 | ||
}, | ||
{ | ||
"client_id": "609788839593-jvlrovi291ge1rblkb7blhvopoas2hpb.apps.googleusercontent.com", | ||
"client_type": 2, | ||
"ios_info": { | ||
"bundle_id": "org.reactjs.native.example.example-chat-rn" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
], | ||
"configuration_version": "1" | ||
} |
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
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
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
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
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
95 changes: 95 additions & 0 deletions
95
.../src/main/java/org/xmtp/android/example/pushnotifications/PushNotificationTokenManager.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package org.xmtp.android.example.pushnotifications | ||
|
||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.content.Context | ||
import android.util.Log | ||
import androidx.annotation.UiThread | ||
import com.google.android.gms.tasks.OnCompleteListener | ||
import com.google.firebase.messaging.FirebaseMessaging | ||
import com.google.firebase.messaging.FirebaseMessagingService | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.runBlocking | ||
import org.xmtp.android.example.R | ||
import org.xmtp.android.library.push.XMTPPush | ||
|
||
object PushNotificationTokenManager { | ||
|
||
private const val TAG = "PushTokenManager" | ||
private lateinit var applicationContext: Context | ||
|
||
private val _xmtpPushState = MutableStateFlow<XMTPPushState>(XMTPPushState.Unknown) | ||
val xmtpPushState: StateFlow<XMTPPushState> = _xmtpPushState | ||
|
||
private var _xmtpPush: XMTPPush? = null | ||
|
||
val xmtpPush: XMTPPush | ||
get() = if (xmtpPushState.value == XMTPPushState.Ready) { | ||
_xmtpPush!! | ||
} else { | ||
throw IllegalStateException("Push not setup") | ||
} | ||
|
||
fun init(applicationContext: Context, pushServer: String) { | ||
this.applicationContext = applicationContext | ||
createXMTPPush(pushServer) | ||
} | ||
|
||
fun ensurePushTokenIsConfigured() { | ||
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { request -> | ||
if (!request.isSuccessful) { | ||
Log.e(TAG, "Firebase getInstanceId() failed", request.exception) | ||
return@OnCompleteListener | ||
} | ||
request.result?.let { | ||
if (xmtpPushState.value is XMTPPushState.Ready) { | ||
xmtpPush.register(it) | ||
configureNotificationChannels() | ||
} | ||
} | ||
}) | ||
} | ||
|
||
internal fun syncPushNotificationsToken(token: String) { | ||
if (xmtpPushState.value is XMTPPushState.Ready) { | ||
runBlocking { xmtpPush.register(token) } | ||
} | ||
} | ||
|
||
private fun configureNotificationChannels() { | ||
val channel = NotificationChannel( | ||
PushNotificationsService.CHANNEL_ID, | ||
applicationContext.getString(R.string.xmtp_direct_message), | ||
NotificationManager.IMPORTANCE_DEFAULT | ||
) | ||
|
||
val notificationManager = applicationContext.getSystemService( | ||
FirebaseMessagingService.NOTIFICATION_SERVICE | ||
) as NotificationManager | ||
notificationManager.createNotificationChannel(channel) | ||
} | ||
|
||
@UiThread | ||
fun createXMTPPush(pushServer: String) { | ||
if (xmtpPushState.value is XMTPPushState.Ready) return | ||
try { | ||
_xmtpPush = XMTPPush(applicationContext, pushServer) | ||
_xmtpPushState.value = XMTPPushState.Ready | ||
} catch (e: Exception) { | ||
_xmtpPushState.value = XMTPPushState.Error(e.localizedMessage.orEmpty()) | ||
} | ||
} | ||
|
||
@UiThread | ||
fun clearXMTPPush() { | ||
_xmtpPushState.value = XMTPPushState.Unknown | ||
_xmtpPush = null | ||
} | ||
|
||
sealed class XMTPPushState { | ||
object Unknown : XMTPPushState() | ||
object Ready : XMTPPushState() | ||
data class Error(val message: String) : XMTPPushState() | ||
} | ||
} |
Oops, something went wrong.