通过Google登录在Android上实现发布/订阅

huangapple go评论58阅读模式
英文:

Pub/Sub on Android via Google Sign-in

问题

I'd like to use Pub/Sub in my Android app. The main problem is that I can't use creds.json in the project. Instead I'd like to use authentication/authorization like I do it when I use the Gmail API. Maybe somebody knows how to do that or can say me that it is impossible.

Let me explain what I have and what I'd like to have.

I have an email client that uses Gmail API to work with messages. I use Google Sign for authentication.

For now, to check for new messages I use CoroutineWorker that uses PeriodicWorkRequest for execution.

Based on docs Periodic work has a minimum interval of 15 minutes. But I'd like to receive updates more frequently and don't do a lot of requests.

I've found that Gmail API can use Push Notifications.

I've done the preparation and I'm able to get updates via Pub/Sub functionality. My Android app can subscribe for updates.

The worked code looks like this

implementation 'io.grpc:grpc-okhttp:1.54.0'```

```kotlin
GlobalScope.launch(Dispatchers.IO) {
      try {
        val subscriptionName = ProjectSubscriptionName.of("myproject", "my-sub")
        val subscriber = Subscriber.newBuilder(subscriptionName,
          MessageReceiver { message, _ ->
            Log.d("PUBSUB", "message = ${message?.data?.toStringUtf8()}")
          })
          .setCredentialsProvider {
            GoogleCredentials.fromStream(resources.openRawResource(R.raw.creds))
          }
          .build()
        subscriber?.startAsync()?.awaitRunning()
        subscriber?.awaitTerminated()
      } catch (t: Throwable) {
        t.printStackTrace()
        Log.i("PUBSUB", t.message ?: "Something went wrong")
      }
    }
英文:

I'd like to use Pub/Sub in my Android app. The main problem is that I can't use creds.json in the project. Instead I'd like to use authenification/authorization like I do it when I use the Gmail API. Maybe somebody knows how to do that or can say me that it is imposible.

Let me explain what I have and what I'd like to have.

I have an email client that uses Gmail API to work with messages. I use Google Sign for authentication.

For now, to check for new messages I use CoroutineWorker that uses PeriodicWorkRequest for execution.

Based on docs Periodic work has a minimum interval of 15 minutes. But I'd like to receive updates more frequently and don't do a lot of requests.

I've found that Gmail API can use Push Notifications.

I've done the preparation and I'm able to get updates via Pub/Sub functionality. My Android app can subscribe for updates.

The worked code looks like this

implementation ('com.google.cloud:google-cloud-pubsub:1.123.7')
implementation 'io.grpc:grpc-okhttp:1.54.0'
GlobalScope.launch(Dispatchers.IO) {
      try {
        val subscriptionName = ProjectSubscriptionName.of("myproject", "my-sub")
        val subscriber = Subscriber.newBuilder(subscriptionName,
          MessageReceiver { message, _ ->
            Log.d("PUBSUB", "message = ${message?.data?.toStringUtf8()}")
          })
          .setCredentialsProvider {
            GoogleCredentials.fromStream(resources.openRawResource(R.raw.creds))
          }
          .build()
        subscriber?.startAsync()?.awaitRunning()
        subscriber?.awaitTerminated()
      } catch (t: Throwable) {
        t.printStackTrace()
        Log.i("PUBSUB", t.message ?: "Something went wrong")
      }
    }


答案1

得分: 0

订阅通过Android设备访问Cloud Pub/Sub将被视为反模式。正如您所指出的,认证模型不适合使用Google登录进行认证。此外,Pub/Sub中的资源数量限制(每个主题或每个项目的订阅限制为10,000个)意味着您只能支持这么多设备/用户。

相反,您应该将Pub/Sub订阅连接到Firebase Cloud Messaging以向最终用户设备发送通知。您可以在消息发布到主题时触发一个函数,然后向设备发送通知。

英文:

Subscribing to Cloud Pub/Sub via Android devices would be considered an anti-pattern. As you noted, the authentication model is not conducive to using Google sign-on for authenticating. Additionally, the limits on number of resources in Pub/Sub (10,000 subscriptions per topic or per project) mean that you'd only be able to support that many devices/users.

Instead, you'd want to connect the Pub/Sub subscription to Firebase Cloud Messaging to send the notifications to the end user devices. You can trigger a function when a message is published to a topic and then send a notification to a device.

huangapple
  • 本文由 发表于 2023年4月11日 16:12:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定