如何获取设备令牌 [英] How to get the device token
问题描述
安装完成后,我需要获取deviceToken用于其他目的.这是我到目前为止开发的:
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();
有人可以帮助我吗?安装完成后,我只需要获取deviceToken.
is there anybody who can help me? I simply need to get the deviceToken after the installation is made.
推荐答案
可能会迟到,但也可能会有所帮助:)
Might be Late but also might be helpfull :)
Parse有时会延迟到何时调用done()方法,我在应用程序运行3分钟并退出后便看到了它的调用.
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();
希望这会有所帮助.
这篇关于如何获取设备令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!