Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HMAC Push Notification Polish #209

Merged
merged 6 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions example/src/main/java/org/xmtp/android/example/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package org.xmtp.android.example

import android.Manifest
import android.accounts.AccountManager
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
Expand All @@ -28,6 +32,7 @@ import org.xmtp.android.example.pushnotifications.PushNotificationTokenManager
import org.xmtp.android.example.utils.KeyUtil
import org.xmtp.android.library.Conversation


class MainActivity : AppCompatActivity(),
ConversationsClickListener {

Expand All @@ -37,10 +42,12 @@ class MainActivity : AppCompatActivity(),
private lateinit var adapter: ConversationsAdapter
private var bottomSheet: NewConversationBottomSheet? = null
private var groupBottomSheet: NewGroupBottomSheet? = null
private val REQUEST_CODE_POST_NOTIFICATIONS = 101

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
accountManager = AccountManager.get(this)
checkAndRequestPermissions()
PushNotificationTokenManager.init(this, "10.0.2.2:8080")
viewModel.setupPush()

Expand Down Expand Up @@ -208,4 +215,18 @@ class MainActivity : AppCompatActivity(),
NewGroupBottomSheet.TAG
)
}

private fun checkAndRequestPermissions() {
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.POST_NOTIFICATIONS
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.POST_NOTIFICATIONS),
REQUEST_CODE_POST_NOTIFICATIONS
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class MainViewModel : ViewModel() {
val listItems = mutableListOf<MainListItem>()
try {
val conversations = ClientManager.client.conversations.list(includeGroups = true)
val hmacKeysResult = ClientManager.client.conversations.getHmacKeys()
val subscriptions = conversations.map {
val hmacKeysResult = ClientManager.client.conversations.getHmacKeys()
val hmacKeys = hmacKeysResult.hmacKeysMap
val result = hmacKeys[it.topic]?.valuesList?.map { hmacKey ->
Service.Subscription.HmacKey.newBuilder().also { sub_key ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,7 @@ class PushNotificationsService : FirebaseMessagingService() {
Manifest.permission.POST_NOTIFICATIONS
) != PackageManager.PERMISSION_GRANTED
) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
Log.e(TAG, "No push notification permissions granted")
return
}
notify(topic.hashCode(), builder.build())
Expand Down
37 changes: 29 additions & 8 deletions library/src/main/java/org/xmtp/android/library/push/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,31 @@ For this tutorial, we'll use [Firebase Cloud Messaging](https://console.firebase

## Run an example notification server

Now that you have an FCM server set up, take a look at the [export-kotlin-proto-code](https://github.com/xmtp/example-notification-server-go/tree/np/export-kotlin-proto-code) branch in the `example-notifications-server-go` repo.
Now that you have an FCM server set up, take a look at the `kotlin` folder in the `example-notifications-server-go` repo.

This example branch can serve as the basis for what you might want to provide for your own notification server. The branch also demonstrates how to generate the proto code if you decide to perform these tasks for your own app. This proto code from the example notification server has already been generated in the `xmtp-android` example app.
These files can serve as the basis for what you might want to provide for your own notification server. This proto code from the example notification server has already been generated and add in the `xmtp-android` example app if you use the example notification server as is.

**To run a notification server based on the example branch:**
**To run a example notification server:**

1. Clone the [example-notification-server-go](https://github.com/xmtp/example-notification-server-go) repo.

2. Complete the steps in [Local Setup](https://github.com/xmtp/example-notification-server-go/blob/np/export-kotlin-proto-code/README.md#local-setup).

3. Get the FCM project ID and `google-services.json` file you created earlier and run:
3. Get the FCM project ID and the FCM credentials you created in step 4 of setting up FCM and run:

```bash
YOURFCMJSON=$(cat /path/to/FCMCredentials.json)
```

```bash
dev/run \
--xmtp-listener-tls \
--xmtp-listener \
--api \
-x "grpc.production.xmtp.network:443:5556" \
-x "grpc.production.xmtp.network:443" \
-d "postgres://postgres:xmtp@localhost:25432/postgres?sslmode=disable" \
--fcm-enabled \
--fcm-credentials-json=YOURFCMJSON \
--fcm-credentials-json=$YOURFCMJSON \
--fcm-project-id="YOURFCMPROJECTID"
```

Expand Down Expand Up @@ -68,7 +72,7 @@ This example branch can serve as the basis for what you might want to provide fo

5. Add the example notification server address to the example app's `MainActivity`. In this case, it should be `PushNotificationTokenManager.init(this, "10.0.2.2:8080")`.

6. Change the example app's environment to `XMTPEnvironment.PRODUCTION` in `Client.kt`.
6. Change the example app's environment to `XMTPEnvironment.PRODUCTION` in `ClientManager.kt`.

7. Set up the example app to register the FCM token with the network and then subscribe each conversation to push notifications. For example:

Expand All @@ -77,7 +81,24 @@ This example branch can serve as the basis for what you might want to provide fo
```

```kotlin
XMTPPush(context, "10.0.2.2:8080").subscribe(conversations.map { it.topic })
val subscriptions = conversations.map {
val hmacKeysResult = ClientManager.client.conversations.getHmacKeys()
val hmacKeys = hmacKeysResult.hmacKeysMap
val result = hmacKeys[it.topic]?.valuesList?.map { hmacKey ->
Service.Subscription.HmacKey.newBuilder().also { sub_key ->
sub_key.key = hmacKey.hmacKey
sub_key.thirtyDayPeriodsSinceEpoch = hmacKey.thirtyDayPeriodsSinceEpoch
}.build()
}

Service.Subscription.newBuilder().also { sub ->
sub.addAllHmacKeys(result)
sub.topic = it.topic
sub.isSilent = it.version == Conversation.Version.V1
}.build()
}

XMTPPush(context, "10.0.2.2:8080").subscribeWithMetadata(subscriptions)
```

```kotlin
Expand Down
Loading