如何获取设备令牌

时间:2015-02-02 08:55:04

标签: android parse-platform push-notification token

安装完成后,我需要将deviceToken用于其他目的。这是我到目前为止开发的内容:

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。

2 个答案:

答案 0 :(得分:0)

代码中没有问题,我猜你是在模拟器中测试这段代码,在模拟器中它没有任何deviceToken,在真实设备中尝试你的代码。

答案 1 :(得分:0)

可能会迟到但也可能有帮助:)

Parse有时会在调用done()方法时出现一些延迟,我在看过应用程序3分钟并退出它之后看到它被调用了。

我对方法调用使用了不同的顺序:

DbContext

希望这有助于某种方式。