Search code examples
codenameone

Is the output of `Push.getPushKey()` always the same?


I have to store server-side the output of Push.getPushKey().

Is it always the same? Or is it better to make a REST request to update its value in the init()?

Can its value be null?

For example, is the following code in the init() a good approach? It waits for a not null PushKey, then it sends it and stops the timer.

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (Push.getPushKey() != null && authToken != null) {
                    Rest.post(Server.getRestServerURL() + "/updatePushKey")
                            .jsonContent()
                            .header("authToken", authToken)
                            .body(Push.getPushKey())
                            .fetchAsString((Response<String> response) -> {
                                if (isSuccessResponse(response)) {
                                    Log.p("PushKey successfully sent to the server", Log.INFO);
                                }
                            });
                    timer.cancel();
                }
            }
        }, 1000, 1000);

Thank you for your clarifications.


Solution

  • Don't use a timer. You need to store this when the registeredForPush callback is invoked. Yes, it can update and it can be null if registration failed.