Forums de discussion

Liferay Push only works for admins and broken for more than 1 user?

thumbnail
Cameron McBride, modifié il y a 8 années.

Liferay Push only works for admins and broken for more than 1 user?

New Member Publications: 8 Date d'inscription: 11/07/12 Publications récentes
When I try to register with Liferay for push notifications they fail with a PrincipalException, unless the user is a portal admin.

Push.with(SessionContext.createSessionFromCurrentSession()).register(getActivity(), "my sender id");

That line works perfectly if I am logged in as "test@liferay.com" but if I log in as a regular user you get hit with the PrincipalException. To reproduce this issue you can add a user, log in, get the error. Next add the Administrator role to the user and try to register again, it will work. This was tried with Liferay 6.2 EE SP10 and Tomcat.

The next issue is if more than one user try to use the application. For example, log in as user1@liferay.com (as an admin) and register. Next log out and log in as user2@liferay.com (also an admin). You will get the following error when the mobile sdk tries to register for push notifications.

W/System.err﹕ org.json.JSONException: Value null at 0 of type org.json.JSONObject$1 cannot be converted to JSONObject
W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:100)
W/System.err﹕ at org.json.JSONArray.getJSONObject(JSONArray.java:514)
thumbnail
Cameron McBride, modifié il y a 8 années.

RE: Liferay Push only works for admins and broken for more than 1 user?

New Member Publications: 8 Date d'inscription: 11/07/12 Publications récentes
I still have not figured out how to get past the permissions issue but I have worked around the double user bug.

1. When the user logs in you need to store the token that is returned by the register method. You're going to need that token later on. It is the GCM registrationId.

2. When the user logs out you can call the unregister method and pass in the token you saved in step 1. That will unregister the user from Liferay push. Make sure you call that before you clear out the Liferay session!

What happens if the user is logged out some other way, maybe their password changes or their session expires? If a second user tries to log in they will still get the error. If you call unregister at this point it won't work. This is because it is also checking the user id, which won't match in this case.

3. If the register command fails you need to call PushNotificationsDeviceLocalServiceUtil.deletePushNotificationsDevice(token). This will delete the token regardless of the user id! I'm calling this from a custom remote service.

My pseudo code looks like this:
if just logged in then
   register for push
   if success then save token
   if fails and has saved token then
      call PushNotificationsDeviceLocalServiceUtil to delete token
      register for push
   end if
end if
thumbnail
Bruno Farache, modifié il y a 8 années.

RE: Liferay Push only works for admins and broken for more than 1 user?

Liferay Master Publications: 603 Date d'inscription: 14/05/07 Publications récentes
Cameron,

The user that is registering the device needs the MANAGE_DEVICES permission: PushNotificationsDeviceServiceImpl.java.