How to get the device token

浪尽此生 提交于 2019-12-22 08:57:08

问题


After the installation has been made I need to get the deviceToken for other purposes. This is what I 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 app crashes and this is the error generated:

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

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

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

is there anybody who can help me? I simply need to get the deviceToken after the installation is made.


回答1:


There is no problem in code, I guess you are testing this code in emulator and in emulator it will not have any deviceToken, try your code in real device.




回答2:


Might be Late but also might be helpfull :)

Parse sometimes has some delays to when the done() method is called, I've seen it being called after I played around with the application for 3 minutes and exit it.

I used a different order for the method calls:

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();

Hope this Helps in some way.



来源:https://stackoverflow.com/questions/28274004/how-to-get-the-device-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!