简体   繁体   English

Android解析推送通知和新GCM生成错误的设备令牌并解析推送通知无法正常工作

[英]Android parse push notification and new GCM generate wrong device token and parse push notification not working

Brief : In parse installation table device token is not added properly when I use new GCM API . 简介 :在解析安装表中,当我使用新的GCM API时,设备令牌未正确添加。 right now following type of device token added into Parse installation table. 现在跟随添加到Parse安装表中的设备令牌类型。

DeviceToken : |ID|1|:crGctxOB068:APA91bFgPRehabJcm9CYdS948iqX2_ppLj02CtbzmEHR0cfbuPooq5F--hqqvR9AH-Ez6MWMQON1Toc2DiNJTNdpRc3nmm3ukIpWJ1jHaXq0Iug6MoHbmKb9U0ak2CrKznkpKnPY5_Jp DeviceToken:| ID | 1 |:crGctxOB068:APA91bFgPRehabJcm9CYdS948iqX2_ppLj02CtbzmEHR0cfbuPooq5F - hqqvR9AH-Ez6MWMQON1Toc2DiNJTNdpRc3nmm3ukIpWJ1jHaXq0Iug6MoHbmKb9U0ak2CrKznkpKnPY5_Jp


Detailed description : 详细说明

I have used new GCM api to get registration id. 我使用新的GCM api获取注册ID。 I need that regId for internal use. 我需要那个内部使用的regId

I have used code from following link of google: Google cloud messaging android . 我使用了谷歌的以下链接代码: 谷歌云消息安卓

I have noted one point. 我注意到了一点。 when ever I start app parse get deviceToken properly. 什么时候我开始应用解析正确获取deviceToken。 After login I am updating "user" field using following code in onCreate of mainActivity 登录后,我使用mainActivity的onCreate中的以下代码更新“user”字段

 ParseACL acl = new ParseACL();
 acl.setPublicReadAccess(true);
 acl.setPublicWriteAccess(true);

 ParseInstallation installation =     ParseInstallation.getCurrentInstallation();
    installation.setACL(acl);

    if (ParseUser.getCurrentUser() != null) {
        installation.put("user", ParseUser.getCurrentUser());
    }
 installation.saveInBackground(new SaveCallback() {
        @Override
        public void done(ParseException e) {
            if (e == null) {
                Log.e("installation", "success");
                Log.i("parse", "token after save : " + ParseInstallation.getCurrentInstallation().getString("deviceToken"));
                ParsePush.subscribeInBackground("", new SaveCallback() {

                    @Override
                    public void done(ParseException e) {

                        if (e != null) {

                            Log.e("error: ", e.getLocalizedMessage());
                            e.printStackTrace();
                        } else {

                            Log.e("subscribed: ", "to broadcast channel");
                            Log.i("parse", "token after subscribe : " + ParseInstallation.getCurrentInstallation().getString("deviceToken"));
                        }
                    }
                });

            } else {
                Log.e("installation", "failed");
                e.printStackTrace();
            }
        }
    });

Generally when above code run deviceToken got changed to Above mentioned token which seems wrong. 通常当上面的代码运行时,deviceToken被改为上面提到的令牌似乎是错误的。 So My push notification is not working. 所以我的推送通知不起作用。

I have solved issue. 我已经解决了问题。

I need to pass GCM device token to other webservice so I have used following code to get token from GCM. 我需要将GCM设备令牌传递给其他Web服务,因此我使用以下代码从GCM获取令牌。

 InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
               String token = instanceID.getToken(CommonUtils.SENDER_ID,
                       GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

After getting token from this code parse's deviceToken changed. 从此代码解析后获取令牌的deviceToken已更改。 So instead of using above code I have used following code to get deviceToken and it solved the issue. 因此,我没有使用上面的代码,而是使用以下代码来获取deviceToken,它解决了这个问题。

ParseInstallation.getCurrentInstallation().getString("deviceToken");

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM