How to get a device token

After the installation has been done, I need to get deviceToken for other purposes. This is what I have developed so far:

Parse.initialize(this, "qqd423WEfwWEF32FewferT434fs323rfRT", "g7Rre4g7gsGRwgGw458Hdf443gFHk534Mrtg34");
    final ParseInstallation currentInstallation = ParseInstallation.getCurrentInstallation();

    currentInstallation.saveInBackground(new SaveCallback() {
        public void done(ParseException e) {
            if (e == null) {
                System.out.println("ok");
                deviceToken = currentInstallation.get("deviceToken").toString();
                System.out.println(deviceToken);
            } else {
                System.out.println("not ok");
            }
        }
    });

The problem is that if I execute the code, the application shuts down and this is an error:

02-02 09:44:17.151    īš• FATAL EXCEPTION: main, PID: 5855 java.lang.NullPointerException

and the piece of code that generates this is this part:

deviceToken = currentInstallation.get("deviceToken").toString();

Is there anyone who can help me? I just need to get deviceToken after installation.

+4
source share
2 answers

There are no problems in the code, I think you are testing this code in the emulator, but it will not have it in the emulator deviceToken, try your code on a real device.

0
source

, :)

, done(), , , 3 .

:

Parse.initialize(this, "*******", "*******");

    ParsePush.subscribeInBackground("", new SaveCallback()
    {
        @Override
        public void done(ParseException e)
        {
            if (null == e)
            {
                ParseInstallation install = ParseInstallation.getCurrentInstallation();
                String token = install.getString("deviceToken");
                if (token != null)
                {
                    //saved it locally and other stuff
                }
                else
                 {
                   //saved a temporary default value locally
                 }
            }
        }
    });
    ParseInstallation.getCurrentInstallation().saveInBackground();

, -.

0

All Articles