android - QuickBlox notifications: push token disappears -
i've been working on android app uses quickblox chatting. chat working fine, encountering difficulties push notifications logged out users.
the problem devices stop receiving notifications. works fine couple of hours, stops working device. full manual uninstall + reinstall (which logs user in again quickblox + gcm) solves problem.
looking @ quickblox admin panel, can see subscriptions lose push token.
user 1 registered on device a. push token has disappeared. http://i.stack.imgur.com/yukyw.png
user 1 registered on device b. push token still present. http://i.stack.imgur.com/yu5iq.png
i register gcm + quickblox push messages once user logged in quickblox. code largely taken quickblox chat sample.
chatservice:
private void logintochat(final context context, final qbuser user, final qbentitycallback callback) { this.qbchatservice.login(user, new qbentitycallbackimpl() { @override public void onsuccess() { checkplayservices(context); try { qbchatservice.startautosendpresence(auto_presence_interval_in_seconds); } catch (smackexception.notloggedinexception e) { e.printstacktrace(); } if (callback != null) { callback.onsuccess(); } } @override public void onsuccess(object result, bundle params) { super.onsuccess(result, params); checkplayservices(context); } @override public void onerror(list errors) { if (callback != null) { callback.onerror(errors); } } }); } private void checkplayservices(context context) { // register gcm after session has been created playserviceshelper playserviceshelper = new playserviceshelper(context); playserviceshelper.checkplayservices(); }
playserviceshelper:
public class playserviceshelper { private static final string property_app_version = "appversion"; private static final string property_reg_id = "registration_id"; private static final string tag = "playserviceshelper"; private static final int play_services_resolution_request = 9000; private googlecloudmessaging googlecloudmessaging; private context activity; private string regid; private string projectnumber; public playserviceshelper(context activity) { this.activity = activity; this.projectnumber = activity.getstring(r.string.gcm_project_number); checkplayservice(); } private void checkplayservice() { // check device play services apk. if check succeeds, proceed // gcm registration. if (checkplayservices()) { googlecloudmessaging = googlecloudmessaging.getinstance(activity); regid = getregistrationid(); if (regid.isempty()) { registerinbackground(); } } else { log.i(tag, "no valid google play services apk found."); } } /** * check device make sure has google play services apk. if * doesn't, display dialog allows users download apk * google play store or enable in device's system settings. */ public boolean checkplayservices() { int resultcode = googleplayservicesutil.isgoogleplayservicesavailable(activity); if (resultcode != connectionresult.success) { if (googleplayservicesutil.isuserrecoverableerror(resultcode)) { googleplayservicesutil.geterrordialog(resultcode, activity, play_services_resolution_request).show(); } return false; } return true; } /** * gets current registration id application on gcm service. * <p/> * if result empty, app needs register. * * @return registration id, or empty string if there no existing * registration id. */ private string getregistrationid() { final sharedpreferences prefs = getgcmpreferences(); string registrationid = prefs.getstring(property_reg_id, ""); if (registrationid.isempty()) { log.i(tag, "registration not found."); return ""; } // check if app updated; if so, must clear registration id // since existing regid not guaranteed work new // app version. int registeredversion = prefs.getint(property_app_version, integer.min_value); int currentversion = getappversioncode(); if (registeredversion != currentversion) { log.i(tag, "app version changed."); return ""; } return registrationid; } /** * registers application gcm servers asynchronously. * <p/> * stores registration id , app versioncode in application's * shared preferences. */ private void registerinbackground() { new asynctask<void, void, string>() { @override protected string doinbackground(void... params) { string msg = ""; try { if (googlecloudmessaging == null) { googlecloudmessaging = googlecloudmessaging.getinstance(activity); } regid = googlecloudmessaging.register(projectnumber); msg = "device registered, registration id=" + regid; // should send registration id server on http, // can use gcm/http or ccs send messages app. handler h = new handler(activity.getmainlooper()); h.post(new runnable() { @override public void run() { subscribetopushnotifications(regid); } }); // persist regid - no need register again. storeregistrationid(regid); } catch (ioexception ex) { msg = "error :" + ex.getmessage(); // if there error, don't keep trying register. // require user click button again, or perform // exponential back-off. } return msg; } @override protected void onpostexecute(string msg) { log.i(tag, msg + "\n"); } }.execute(null, null, null); } /** * @return application's {@code sharedpreferences}. */ private sharedpreferences getgcmpreferences() { return activity.getsharedpreferences(activity.getpackagename(), context.mode_private); } /** * subscribe push notifications * * @param regid registration id */ private void subscribetopushnotifications(string regid) { string deviceid; final telephonymanager mtelephony = (telephonymanager) activity.getsystemservice(context.telephony_service); if (mtelephony.getdeviceid() != null) { deviceid = mtelephony.getdeviceid(); } else { deviceid = settings.secure.getstring(activity.getcontentresolver(), settings.secure.android_id); } qbmessages.subscribetopushnotificationstask(regid, deviceid, qbenvironment.production, new qbentitycallbackimpl<arraylist<qbsubscription>>() { @override public void onsuccess(arraylist<qbsubscription> qbsubscriptions, bundle bundle) { } @override public void onerror(list<string> strings) { } }); } /** * stores registration id , app versioncode in application's * {@code sharedpreferences}. * * @param regid registration id */ private void storeregistrationid(string regid) { final sharedpreferences prefs = getgcmpreferences(); int appversion = getappversioncode(); sharedpreferences.editor editor = prefs.edit(); editor.putstring(property_reg_id, regid); editor.putint(property_app_version, appversion); editor.apply(); } private int getappversioncode() { try { packageinfo packageinfo = this.activity.getpackagemanager().getpackageinfo(this.activity.getpackagename(), 0); return packageinfo.versioncode; } catch (packagemanager.namenotfoundexception e) { return 0; } } }
to allow user receive notifications, log him out every time app goes background (using onstop in baseactivity) , log in when app resumes (which again starts playserviceshelper).
chatservice.getinstance().logout(null);
the app uses parse (with gcm) push notifications, problem still seemed occur when disabled parse.
could problem caused having 1 user registered on multiple devices @ same time? frequent app installs (without manually removing previous installation) result in behavior?
push token can disappear if 2 users use same device
for example, user 1 subscribed pushes on devicea
then user 2 subscribed pushes on devicea (the same device)
after user 2 receiving pushes, not user 1
is there chance have such logic in case?
Comments
Post a Comment