diff --git a/example/src/main/java/org/xmtp/android/example/MainActivity.kt b/example/src/main/java/org/xmtp/android/example/MainActivity.kt index 5c0460dd7..e85b5025a 100644 --- a/example/src/main/java/org/xmtp/android/example/MainActivity.kt +++ b/example/src/main/java/org/xmtp/android/example/MainActivity.kt @@ -1,10 +1,12 @@ 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 @@ -12,6 +14,8 @@ 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 @@ -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 { @@ -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() @@ -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 + ) + } + } } diff --git a/example/src/main/java/org/xmtp/android/example/MainViewModel.kt b/example/src/main/java/org/xmtp/android/example/MainViewModel.kt index 4a62ef9c4..99bd24111 100644 --- a/example/src/main/java/org/xmtp/android/example/MainViewModel.kt +++ b/example/src/main/java/org/xmtp/android/example/MainViewModel.kt @@ -45,8 +45,8 @@ class MainViewModel : ViewModel() { val listItems = mutableListOf() 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 -> diff --git a/example/src/main/java/org/xmtp/android/example/pushnotifications/PushNotificationsService.kt b/example/src/main/java/org/xmtp/android/example/pushnotifications/PushNotificationsService.kt index 83ce6a5ed..b87e2fc1f 100644 --- a/example/src/main/java/org/xmtp/android/example/pushnotifications/PushNotificationsService.kt +++ b/example/src/main/java/org/xmtp/android/example/pushnotifications/PushNotificationsService.kt @@ -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()) diff --git a/library/src/main/java/org/xmtp/android/library/push/README.md b/library/src/main/java/org/xmtp/android/library/push/README.md index 5cd83806e..7f92cb43b 100644 --- a/library/src/main/java/org/xmtp/android/library/push/README.md +++ b/library/src/main/java/org/xmtp/android/library/push/README.md @@ -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" ``` @@ -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: @@ -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 hmacKeysResult = ClientManager.client.conversations.getHmacKeys() + val subscriptions = conversations.map { + 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