如何获取设备令牌

5

安装完成后,我需要获取设备令牌以供其他用途。以下是我目前开发的内容:

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");
            }
        }
    });

问题在于,如果我执行这段代码,应用程序就会崩溃,这是生成的错误:
02-02 09:44:17.151    ﹕ FATAL EXCEPTION: main, PID: 5855 java.lang.NullPointerException

生成此代码的代码段如下:

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

有没有人可以帮助我?我只需要在安装完成后获取设备标识符(deviceToken)。


如果 currentInstallation 为空或者 currentInstallation.get("deviceToken") 为空,则... - 18446744073709551615
@18446744073709551615 是的,这就是情况:currentInstallation.get("deviceToken") 为空。但如果我在 SaveCallBack 方法中调用它,为什么它应该为空呢? - ernestocattaneo
感谢,已获取设备令牌... - Najib.Nj
2个回答

0

可能有点晚,但也许会有帮助 :)

解析器有时会延迟调用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

代码没有问题,我猜您是在模拟器中测试此代码,并且在模拟器中它将没有任何设备标记,请尝试在实际设备上运行您的代码。


始终在真实设备上进行测试。 - ernestocattaneo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接