简体   繁体   English

如何在Android库模块中包含GCM / FCM?

[英]How to include GCM/FCM in an android library module?

Am not able to generate google-services json for an android library because its asking for the application id. 无法为Android库生成google-services json,因为它要求提供应用程序ID。 I know how to add it in a project. 我知道如何在项目中添加它。

FCM client setup FCM客户端设置

Libraries don't have an application ID. 图书馆没有应用程序ID。 Application ID is supplied by the consumer of your library. 应用程序ID由库的使用者提供。 Just include the dependency and that's it. 只需包含依赖项即可。 Don't apply the google services plugin on a library. 不要将Google Services插件应用于库。

Your client would have presumably followed https://firebase.google.com/docs/cloud-messaging/android/client tutorial. 您的客户可能会遵循https://firebase.google.com/docs/cloud-messaging/android/client教程。 Which means you have to create your library's push message processor to which your client will delegate work. 这意味着您必须创建库的推送消息处理器,客户端将工作委托给该处理器。 Example: 例:

your.client.app.package.fcm.MyFirebaseMessagingService.kt your.client.app.package.fcm.MyFirebaseMessagingService.kt

class MyFirebaseMessagingService() : FirebaseMessagingService() {
    override fun onMessageReceived(p0: RemoteMessage) {
        super.onMessageReceived(p0)

        // Your client's message handling.
        // ...

        // If not handled by client, delegate to library.
        AwesomeLibrary.processPushMessage(this, p0)
}

your.awesome.library.package.fcm.AwesomeLibraryFcm.kt your.awesome.library.package.fcm.AwesomeLibraryFcm.kt

object AwesomeLibraryFcm {
    fun processPushMessage(context: Context, message: RemoteMessage) {
        // TODO Your job.
    }
}

As you can see connecting Firebase to your library is handled entirely by your library's consumer. 如您所见,将Firebase连接到库完全由库的使用者处理。 You don't touch Firebase on client side at all. 您根本不需要触摸Firebase。

FCM server setup FCM服务器设置

You need an application ID to setup FCM. 您需要一个应用程序ID才能设置FCM。 One application ID can only be associated to one Firebase Project, which would be that of your library's consumer. 一个应用程序ID只能与一个Firebase项目相关联,这就是您图书馆的使用者的ID。 Dead end. 死路。

If you're running an online service and need to send push messages on behalf of your client you need them to create a Server key (restricted to your server's IP address) in Google Dev console and upload it along with Project ID to your server. 如果您正在运行在线服务,并且需要代表客户发送推送消息,则需要他们在Google Dev控制台中创建服务器密钥 (仅限于服务器的IP地址),并将其与Project ID一起上传到您的服务器。 These would allow your server to send push messages. 这些将允许您的服务器发送推送消息。 This is essentially your client giving you permission to send push mesages on their behalf . 从本质上讲,这是您的客户允许您代表他们发送推送消息的权限

Mind potential security risks and abuse of your service. 注意潜在的安全风险和滥用服务。

I integrated FCM in library project and used that library in a new app project. 我将FCM集成到库项目中,并在新的应用程序项目中使用了该库。 I copied google service json file from library and paste it in new app project's app module. 我从库中复制了Google服务json文件,并将其粘贴到新应用程序项目的应用程序模块中。 After this I modified this google service json file. 之后,我修改了这个Google服务json文件。 I renamed "package_name" under "client_info" to app package name and I was able to get notification. 我将"package_name" "client_info" "package_name"下的"package_name"重命名为应用程序包名称,并且能够收到通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM