Development notes-Android message push show operation: Amazon-SNS docking FCM and Baidu cloud push

Written in front:

        I used Aliyun’s server to push Ali’s message well before, but because foreign business needs to use Amazon’s server, Alibaba Cloud’s push can’t be used. I use Amazon’s SNS (Short Message Push Service), and SNS can connect to multiple push platforms, among which Android There are two FCM and Baidu cloud. FCM is the son of Google, but it can’t get in without a ladder. Baidu can use it in China but can’t get out of it abroad. For this reason both pushes are connected. It is easy to access Baidu Push or FCM separately. The main reason is that the SNS documents connected to Amazon are all in English and the description is not clear, and the information found on the Internet is scattered, so I recorded the connection process this time.

1. Single channel access

        Baidu Cloud Push Official Documents  Baidu Cloud Push needs to create an application first. These materials are relatively easy to find and will not be repeated. After the creation is successful, these parameters are mainly provided to Amazon. The client key is the corresponding SECRET KEY

 The official document  flow of FCM is very clear, and it feels more convenient than Baidu's access, and there is no need to configure the .so library. To create an application, you need to provide an API key after creation. In fact, it is the server key in the cloud messaging in the picture below.

 

 

 2. Access to SNS

        At this point, it is considered that your push integration is successful, on the premise that there is no problem in testing the push on Baidu Cloud/FCM's own console. Ready to connect our device with SNS. The most important thing is to create an endpoint and then pass the endpoint information to the server. The Android side needs to introduce SNS-related packages (currently the latest version)

    implementation 'com.amazonaws:aws-android-sdk-sns:2.6.20'

        Baidu Cloud Push requires two attributes, token and UserId, to create an endpoint. These two parameters will be obtained when the Baidu Cloud Push service is successfully bound. In the official code, there is no special method to add extended attributes, and parameters need to be added through the withAttributes() method. Instead of this method, FCM can directly pass the token obtained by FCM's onNewToken through withToken(). At the same time, you can also subscribe to the topic, but it is commented out here, and you can let it go if necessary. The created endpoint information needs to be handed over to the server, and the server will make corresponding pushes. As for how to give it, it is a matter of agreement between the front and back ends. After the endpoint is successfully created, it can be seen in the background of the Amazon server.

        

fun createBaiduEndpoint(userId:String?,token: String): String? {
    Log.e(PushMessageReceiver.TAG, "sendTokentoSNS: ")
    userId?:return null
    val access_id = "你的亚马逊access_id"
    val secret_key = "你的亚马逊secret_key"
    val appArn = "亚马逊创建的应用的ARN"
//  val topicArn= "亚马逊创建的应用的主题的ARN"
    val credentials: AWSCredentials = BasicAWSCredentials(access_id, secret_key)
    val provider: AWSCredentialsProvider = StaticCredentialsProvider(credentials)
    val snsClient = AmazonSNSClient(provider)
    
    snsClient.setRegion(Region.getRegion(Regions.EU_WEST_1))
    var endpointArn: String? = null
    endpointArn = try {
        println("Creating platform endpoint with token $token")
            //百度云推送在绑定的时候会返回的参数userId和channeId
        val mutableMapOf = mutableMapOf<String, String>()
        mutableMapOf["UserId"]=userId
        mutableMapOf["ChannelId"]=token
        val cpeReq = CreatePlatformEndpointRequest()
        .withPlatformApplicationArn(appArn)
        .withToken(token)
        .withAttributes(mutableMapOf)//百度需要FCM不用

         val cpeRes: CreatePlatformEndpointResult = snsClient
             .createPlatformEndpoint(cpeReq)
//        val subscribeRequest: SubscribeRequest = SubscribeRequest()
//            .withProtocol("application")
//            .withTopicArn(topicArn)
//            .withEndpoint(result.endpointArn)
//        snsClient.subscribe(subscribeRequest)
            return cpeRes.endpointArn
     } catch (ipe: InvalidParameterException) {
         val message: String = ipe.getErrorMessage()
         ipe.printStackTrace()
         val p: Pattern = Pattern
             .compile(
              ".*Endpoint (arn:aws:sns[^ ]+) already exists " +
                  "with the same [Tt]oken.*"
              )
         val m: Matcher = p.matcher(message)
         if (m.matches()) {
                // The platform endpoint already exists for this token, but with
                // additional custom data that
                // createEndpoint doesn't want to overwrite. Just use the
                // existing platform endpoint.
             m.group(1)
         } else {
                // Rethrow the exception, the input is actually bad.
             throw ipe
         }
     }
     return endpointArn
}

      Write on the back:

        The technical content of the article is not at all, mainly because there is not a complete set of information on the Internet, and I have even searched on Baidu for a long time on how to introduce dependencies. Some of the above codes are even written by my own guesses from the issue on the ruby ​​side of git. There are relatively many related FCM (GCM) connected to SNS on the Internet. I also mainly learn from this blog about the use of AWS SNS+Google FCM push service . It is a pity that it does not talk about how to introduce dependencies. However, there are very few reports about Baidu's access to SNS. Writing it here is convenient for myself to review in the future, and I hope it will make others less likely to step on such pitfalls.

Guess you like

Origin blog.csdn.net/qq_37841321/article/details/120078857