gpt4 book ai didi

android - Grails Firebase 云消息传递插件

转载 作者:行者123 更新时间:2023-12-02 14:59:53 25 4
gpt4 key购买 nike

我正在寻找将发送下游 Android GCM 通知的应用程序服务器移植到 Firebase Cloud Messaging。我目前正在使用 Grails 的 Android GCM 插件来发送下游消息。

https://grails.org/plugin/android-gcm

我没有看到用于发送下游消息的 Grails FCM 插件。我错过了什么还是它还不存在?

最佳答案

我决定直接从 Grails 发出一个 Http 请求。我没有足够的声誉来发布超过 2 个链接,所以我将它们包装在代码示例中,抱歉。每

https://firebase.google.com/docs/cloud-messaging/http-server-ref#interpret-downstream

Errors in the 500-599 range (such as 500 or 503) indicate that there was an internal error in the FCM connection server while trying to process the request, or that the server is temporarily unavailable (for example, because of timeouts). Sender must retry later, honoring any Retry-After header included in the response. Application servers must implement exponential back-off.



所以我决定使用 Google 的 Http Java Client,它提供指数回退
https://developers.google.com/api-client-library/java/google-http-java-client/
https://developers.google.com/api-client-library/java/google-http-java-client/backoff

对于用例示例,添加依赖项:
compile 'com.google.http-client:google-http-client:1.22.0'
compile 'com.google.http-client:google-http-client-jackson2:1.22.0'

然后使用 HTTP Post Request 示例实现一个简单的 Controller
https://firebase.google.com/docs/cloud-messaging/downstream


import com.google.api.client.http.javanet.NetHttpTransport
import com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler
import com.google.api.client.http.HttpTransport
import com.google.api.client.http.HttpRequest
import com.google.api.client.http.HttpResponse
import com.google.api.client.http.GenericUrl
import com.google.api.client.http.HttpUnsuccessfulResponseHandler;
import com.google.api.client.http.json.JsonHttpContent
import com.google.api.client.json.jackson2.JacksonFactory
import com.google.api.client.util.ExponentialBackOff
import com.google.api.client.http.HttpHeaders
import com.google.api.client.http.HttpResponseException

class MyPushController {

def push(String token){

def postJson = [:]

def data = [:]
data['score'] = '5x1'
data['time'] = '15:10'

postJson['data'] = data
postJson['to'] = token

log.info "postJson: ${postJson}"

HttpTransport transport = new NetHttpTransport()

HttpRequest request = transport.createRequestFactory().buildPostRequest(new GenericUrl("https://fcm.googleapis.com/fcm/send"), new JsonHttpContent(new JacksonFactory(), postJson));

HttpHeaders reqHeaders = new HttpHeaders()
reqHeaders.setAuthorization("key=${grailsApplication.config.android.fcm.api.key}")
reqHeaders.setAccept("application/json")
reqHeaders.setContentType("application/json")

request.setHeaders(reqHeaders)

request.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff.Builder()
.setInitialIntervalMillis(500)
.setMaxElapsedTimeMillis(900000)
.setMaxIntervalMillis(6000)
.setMultiplier(1.5)
.setRandomizationFactor(0.5)
.build()
))

try{

HttpResponse response = request.execute();

InputStream is = response.getContent()
BufferedReader br = new BufferedReader(new InputStreamReader(is))

StringBuilder sb = new StringBuilder();

String line = null;
try {
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}

ObjectMapper mapper = new ObjectMapper()
Map<String, Object> responseMap = mapper.readValue(sb.toString(), new TypeReference<Map<String, Object>>(){})

// Process response JSON per https://firebase.google.com/docs/cloud-messaging/server#response
if(responseMap && (responseMap['failure'] != 0 || responseMap['canonical_ids'] != 0)){
if(responseMap['message_id'] && responseMap['registration_id']){

log.info "New push token, setting to ${responseMap['registration_id']}"

// TODO Notify backend that token has changed, i.e. update

}

}else{

def results = responseMap['results']

if(results){

results.each{

if(it['error']){

if(it['error'] == "NotRegistered"){

log.info 'NotRegistered, updating AppToken to null'

// TODO Notify backend this token is no longer valid, i.e. delete

}
}

}

}

}
}

render responseMap as JSON

}catch(HttpResponseException e){

log.error "Error: ${e.toString()}"

render (['SC' : e.getStatusCode(), 'M' : e.getStatusMessage() ]) as JSON

}

}

}

请务必将 ${grailsApplication.config.android.fcm.api.key} 替换为您的 FCM 服务器 key 。您可以通过登录 Firebase 控制台找到您的 FCM 服务器 key :
https://console.firebase.google.com

然后转到项目设置 -> 云消息

关于android - Grails Firebase 云消息传递插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37867597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com