Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. right now following type of device token added into Parse installation table.

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


Detailed description :

I have used new GCM api to get registration id. I need that regId for internal use.

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. After login I am updating "user" field using following code in onCreate of mainActivity

 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. So My push notification is not working.

like image 410
Kalpesh Gohel Avatar asked Jul 29 '15 06:07

Kalpesh Gohel


1 Answers

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.

 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. So instead of using above code I have used following code to get deviceToken and it solved the issue.

ParseInstallation.getCurrentInstallation().getString("deviceToken");
like image 194
Kalpesh Gohel Avatar answered Oct 17 '22 23:10

Kalpesh Gohel