简体   繁体   English

如何获取设备令牌

[英]How to get the device token

After the installation has been made I need to get the deviceToken for other purposes.安装完成后,我需要获取 deviceToken 用于其他目的。 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.我只需要在安装完成后获取 deviceToken。

代码没有问题,我猜您正在模拟器中测试此代码,而在模拟器中它不会有任何deviceToken ,请在真实设备中尝试您的代码。

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. Parse 有时会在调用 done() 方法时出现一些延迟,我已经看到在我使用应用程序 3 分钟并退出它之后调用它。

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.希望这在某种程度上有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用phonegap和pushbots获取android设备令牌 - how to get android device token with phonegap and pushbots 如何获取用于推送通知的 Android 设备令牌? - How to get Android device token for push notifications? 获取Android设备令牌 - Get Android device token 如何在 Firebase 云消息传递的 Kotlin 中获取 Android 设备令牌 - How to get Android Device Token in Kotlin for Firebase Cloud Messaging 如何为卸载应用程序的设备获取FCM的令牌ID? - How to get the token id for FCM for the device which uninstall the application? 使用推送通知时如何获取用户设备令牌 - how to get the users device token when using push-notification 如何获取华为 Api 的访问令牌以向设备发送推送? - How to get access token for Huawei Api for sending push to device? 如何从 laravel 获取设备令牌以发送推送通知? - How to get the device token from laravel to send push notification? 我们如何获得Android的设备令牌以进行推送通知? - how can we get device token for android for push notification? 在React Native中注册时如何获取设备令牌 - How to get the device token at the time of registration in react native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM