Cordova Firebase插件 - 未在iOS上的通知栏中显示通知

时间:2016-12-27 14:19:03

标签: ios cordova push-notification firebase-cloud-messaging

我正在使用这个插件 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() ])

    }
}
}

1 个答案:

答案 0 :(得分:0)

我明白了,实际上我想念上传APNs证书

console.firebase.google.com 

您可以参考该链接了解更多详情。 https://firebase.google.com/docs/cloud-messaging/ios/client