Skip to content

Commit 5cd7706

Browse files
committed
MOBILE-3523 push: Allow push with siteurl or no site
1 parent f2df105 commit 5cd7706

File tree

2 files changed

+58
-69
lines changed

2 files changed

+58
-69
lines changed

src/core/pushnotifications/providers/pushnotifications.ts

Lines changed: 40 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,19 @@ export class CorePushNotificationsProvider {
457457
*/
458458
onMessageReceived(notification: any): void {
459459
const data = notification ? notification.additionalData : {};
460+
let promise;
461+
462+
if (data.site) {
463+
promise = this.sitesProvider.getSite(data.site);
464+
} else if (data.siteurl) {
465+
promise = this.sitesProvider.getSiteByUrl(data.siteurl);
466+
} else {
467+
// Notification not related to any site.
468+
promise = Promise.resolve();
469+
}
460470

461-
this.sitesProvider.getSite(data.site).then((site) => {
471+
promise.then((site: CoreSite | undefined) => {
472+
data.site = site && site.getId();
462473

463474
if (typeof data.customdata == 'string') {
464475
data.customdata = this.textUtils.parseJSON(data.customdata, {});
@@ -468,80 +479,40 @@ export class CorePushNotificationsProvider {
468479
// If the app is in foreground when the notification is received, it's not shown. Let's show it ourselves.
469480
if (this.localNotificationsProvider.isAvailable()) {
470481
const localNotif: ILocalNotification = {
471-
id: data.notId || 1,
472-
data: data,
473-
title: '',
474-
text: '',
475-
channel: 'PushPluginChannel'
476-
},
477-
options = {
478-
clean: true,
479-
singleLine: true,
480-
contextLevel: 'system',
481-
instanceId: 0,
482-
filter: true
483-
},
484-
isAndroid = CoreApp.instance.isAndroid(),
485-
extraFeatures = this.utils.isTrueOrOne(data.extrafeatures);
486-
487-
// Get the filters to apply to text and message. Don't use FIlterHelper to prevent circular dependencies.
488-
this.filterProvider.canGetFilters(site.getId()).then((canGet) => {
489-
if (!canGet) {
490-
// We cannot check which filters are available, apply them all.
491-
return this.filterDelegate.getEnabledFilters(options.contextLevel, options.instanceId);
492-
}
493-
494-
return this.filterProvider.getAvailableInContext(options.contextLevel, options.instanceId, site.getId());
495-
}).catch(() => {
496-
return [];
497-
}).then((filters) => {
498-
const promises = [];
499-
500-
// Apply formatText to title and message.
501-
promises.push(this.filterProvider.formatText(notification.title, options, filters, site.getId())
502-
.then((title) => {
503-
localNotif.title = title;
504-
}).catch(() => {
505-
localNotif.title = notification.title;
506-
}));
507-
508-
promises.push(this.filterProvider.formatText(notification.message, options, filters, site.getId())
509-
.catch(() => {
510-
// Error formatting, use the original message.
511-
return notification.message;
512-
}).then((formattedMessage) => {
513-
if (extraFeatures && isAndroid && this.utils.isFalseOrZero(data.notif)) {
514-
// It's a message, use messaging style. Ionic Native doesn't specify this option.
515-
(<any> localNotif).text = [
516-
{
517-
message: formattedMessage,
518-
person: data.conversationtype == 2 ? data.userfromfullname : ''
519-
}
520-
];
521-
} else {
522-
localNotif.text = formattedMessage;
482+
id: data.notId || 1,
483+
data: data,
484+
title: notification.title,
485+
text: notification.message,
486+
channel: 'PushPluginChannel'
487+
};
488+
const isAndroid = CoreApp.instance.isAndroid();
489+
const extraFeatures = this.utils.isTrueOrOne(data.extrafeatures);
490+
491+
if (extraFeatures && isAndroid && this.utils.isFalseOrZero(data.notif)) {
492+
// It's a message, use messaging style. Ionic Native doesn't specify this option.
493+
(<any> localNotif).text = [
494+
{
495+
message: notification.message,
496+
person: data.conversationtype == 2 ? data.userfromfullname : ''
523497
}
524-
}));
498+
];
499+
}
525500

526-
if (extraFeatures && isAndroid) {
527-
// Use a different icon if needed.
528-
localNotif.icon = notification.image;
529-
// This feature isn't supported by the official plugin, we use a fork.
530-
(<any> localNotif).iconType = data['image-type'];
501+
if (extraFeatures && isAndroid) {
502+
// Use a different icon if needed.
503+
localNotif.icon = notification.image;
504+
// This feature isn't supported by the official plugin, we use a fork.
505+
(<any> localNotif).iconType = data['image-type'];
531506

532-
localNotif.summary = data.summaryText;
507+
localNotif.summary = data.summaryText;
533508

534-
if (data.picture) {
535-
localNotif.attachments = [data.picture];
536-
}
509+
if (data.picture) {
510+
localNotif.attachments = [data.picture];
537511
}
512+
}
538513

539-
Promise.all(promises).then(() => {
540-
this.localNotificationsProvider.schedule(localNotif, CorePushNotificationsProvider.COMPONENT, data.site,
541-
true);
542-
});
543-
544-
});
514+
this.localNotificationsProvider.schedule(localNotif, CorePushNotificationsProvider.COMPONENT, data.site || '',
515+
true);
545516
}
546517

547518
// Trigger a notification received event.

src/providers/sites.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,24 @@ export class CoreSitesProvider {
13061306
}
13071307
}
13081308

1309+
/**
1310+
* Finds a site with a certain URL. It will return the first site found.
1311+
*
1312+
* @param siteUrl The site URL.
1313+
* @return Promise resolved with the site.
1314+
*/
1315+
async getSiteByUrl(siteUrl: string): Promise<CoreSite> {
1316+
await this.dbReady;
1317+
1318+
const data = await this.appDB.getRecord(CoreSitesProvider.SITES_TABLE, { siteUrl });
1319+
1320+
if (typeof this.sites[data.id] != 'undefined') {
1321+
return this.sites[data.id];
1322+
}
1323+
1324+
return this.makeSiteFromSiteListEntry(data);
1325+
}
1326+
13091327
/**
13101328
* Create a site from an entry of the sites list DB. The new site is added to the list of "cached" sites: this.sites.
13111329
*

0 commit comments

Comments
 (0)