jeudi 25 mai 2017

How to get Firebase messaging registration token in Firebase Admin SDK - Java

I have huge problems with Firebase sdk. I implemented notifications in no-time, but when I started to write tests I realized I had to include Firebase SDK in my Spring project in order to test it properly. Then I tried getting user registration token, but with no luck whatsoever. Even when I get token and try to register it with Firebase, I get response No valid registration ids. Sample of my code is here (tests are written in Groovy but that shouldn't matter).

def "Add token"() {
    given:
    getTokenTask = FirebaseAuth.getInstance(firebaseApp).createCustomToken(user.getId())
            .addOnSuccessListener(new OnSuccessListener<String>() {
        @Override
        public void onSuccess(String customToken) {
            println "custom token " + customToken

        }
    });

    when:
    while(!getTokenTask.isSuccessful()) {}
    token = getTokenTask.getResult().getToken()
    def resp = new RESTClient(apiUrl, ContentType.JSON).post([path: "notifications/token/add",
                                            body: [token: token],
                                            requestContentType: ContentType.JSON,
                                            headers: ["Content-Type":"application/json", "Authorization": user.auth.token]]) { resp ->
        println("resp - " + resp)

    }
    addTokenResponse = resp.toString()

    then:
    assert addTokenResponse == 's'
}

Is it possible in Java on server, or just with Android/iOS sdks? Those packages have separate FirebaseMessaging classes with extra functionality.

I can't push it to production without any tests and I can't find any way to create valid registration tokens.

Aucun commentaire:

Enregistrer un commentaire