diff --git a/src/core/pushnotifications/providers/pushnotifications.ts b/src/core/pushnotifications/providers/pushnotifications.ts index 3a48abfc7..430d78000 100644 --- a/src/core/pushnotifications/providers/pushnotifications.ts +++ b/src/core/pushnotifications/providers/pushnotifications.ts @@ -200,6 +200,15 @@ export class CorePushNotificationsProvider { }); } + /** + * Check whether the device can be registered in Moodle to receive push notifications. + * + * @return {boolean} Whether the device can be registered in Moodle. + */ + canRegisterOnMoodle(): boolean { + return this.pushID && this.appProvider.isMobile(); + } + /** * Delete all badge records for a given site. * @@ -658,7 +667,7 @@ export class CorePushNotificationsProvider { registerDeviceOnMoodle(siteId?: string, forceUnregister?: boolean): Promise { this.logger.debug('Register device on Moodle.'); - if (!this.pushID || !this.appProvider.isMobile()) { + if (!this.canRegisterOnMoodle()) { return Promise.reject(null); } @@ -729,7 +738,7 @@ export class CorePushNotificationsProvider { if (siteId) { // Check if the site has a pending unregister. - promise = this.appDB.getRecords(CorePushNotificationsProvider.REGISTERED_DEVICES_TABLE, {siteid: siteId}); + promise = this.appDB.getRecords(CorePushNotificationsProvider.PENDING_UNREGISTER_TABLE, {siteid: siteId}); } else { // Get all pending unregisters. promise = this.appDB.getAllRecords(CorePushNotificationsProvider.PENDING_UNREGISTER_TABLE); diff --git a/src/core/pushnotifications/providers/register-cron-handler.ts b/src/core/pushnotifications/providers/register-cron-handler.ts index 0f04874b5..e2bc48b2c 100644 --- a/src/core/pushnotifications/providers/register-cron-handler.ts +++ b/src/core/pushnotifications/providers/register-cron-handler.ts @@ -42,7 +42,7 @@ export class CorePushNotificationsRegisterCronHandler implements CoreCronHandler * @return {Promise} Promise resolved when done, rejected if failure. */ execute(siteId?: string): Promise { - if (!siteId) { + if (!siteId || !this.pushNotificationsProvider.canRegisterOnMoodle()) { // It's not a specific site, don't do anything. return Promise.resolve(); } diff --git a/src/providers/utils/utils.ts b/src/providers/utils/utils.ts index d3d09bf47..e1595c75b 100644 --- a/src/providers/utils/utils.ts +++ b/src/providers/utils/utils.ts @@ -742,11 +742,11 @@ export class CoreUtilsProvider { * @return {boolean} Whether the error was returned by the WebService. */ isWebServiceError(error: any): boolean { - return typeof error.warningcode != 'undefined' || (typeof error.errorcode != 'undefined' && + return error && (typeof error.warningcode != 'undefined' || (typeof error.errorcode != 'undefined' && error.errorcode != 'invalidtoken' && error.errorcode != 'userdeleted' && error.errorcode != 'upgraderunning' && error.errorcode != 'forcepasswordchangenotice' && error.errorcode != 'usernotfullysetup' && error.errorcode != 'sitepolicynotagreed' && error.errorcode != 'sitemaintenance' && - (error.errorcode != 'accessexception' || error.message.indexOf('Invalid token - token expired') == -1)); + (error.errorcode != 'accessexception' || error.message.indexOf('Invalid token - token expired') == -1))); } /**