【发布时间】:2017-05-11 20:07:57
【问题描述】:
我正在将此插件与 cordova 一起使用:cordova-plugin-firebase 0.1.18 "Google Firebase Plugin" 它在 android 上运行良好,也在 ios 上我使用方法获得令牌
window.FirebasePlugin.getToken
但是通知栏没有收到通知,因为它在前台或后台调用通知永远不会使用方法进入
window.FirebasePlugin.onNotificationOpen
在服务器端我是这样发布的,
def sendNotfMessage(def registrationIds,def body,def title){
def data = [:]
data['body']=body
data['title']=title
data['icon']="myicon"
data['sound']="mySound"
def postJson = [:]
postJson['priority'] = "high"
postJson['notification'] = data
postJson['registration_ids']=registrationIds
return sendFcmNotf(postJson)
}
def sendFcmNotf(def 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
}
}
}
}
}
return responseMap
}catch(HttpResponseException e){
log.error "Error: ${e.toString()}"
return (['SC' : e.getStatusCode(), 'M' : e.getStatusMessage() ])
}
}
}
【问题讨论】:
-
我知道了,实际上我想在 console.firebase.google.com 上传 APNs 证书谢谢大家
-
你应该继续answer your own question。 :)
标签: ios cordova push-notification firebase-cloud-messaging