Merge pull request #2588 from dpalou/MOBILE-3523

MOBILE-3523 push: Allow push with siteurl or no site
main
Juan Leyva 2020-11-05 13:39:42 +01:00 committed by GitHub
commit b66935cb49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 68 deletions

View File

@ -457,8 +457,19 @@ export class CorePushNotificationsProvider {
*/
onMessageReceived(notification: any): void {
const data = notification ? notification.additionalData : {};
let promise;
this.sitesProvider.getSite(data.site).then((site) => {
if (data.site) {
promise = this.sitesProvider.getSite(data.site);
} else if (data.siteurl) {
promise = this.sitesProvider.getSiteByUrl(data.siteurl);
} else {
// Notification not related to any site.
promise = Promise.resolve();
}
promise.then((site: CoreSite | undefined) => {
data.site = site && site.getId();
if (typeof data.customdata == 'string') {
data.customdata = this.textUtils.parseJSON(data.customdata, {});
@ -470,58 +481,22 @@ export class CorePushNotificationsProvider {
const localNotif: ILocalNotification = {
id: data.notId || 1,
data: data,
title: '',
text: '',
title: notification.title,
text: notification.message,
channel: 'PushPluginChannel'
},
options = {
clean: true,
singleLine: true,
contextLevel: 'system',
instanceId: 0,
filter: true
},
isAndroid = CoreApp.instance.isAndroid(),
extraFeatures = this.utils.isTrueOrOne(data.extrafeatures);
};
const isAndroid = CoreApp.instance.isAndroid();
const extraFeatures = this.utils.isTrueOrOne(data.extrafeatures);
// Get the filters to apply to text and message. Don't use FIlterHelper to prevent circular dependencies.
this.filterProvider.canGetFilters(site.getId()).then((canGet) => {
if (!canGet) {
// We cannot check which filters are available, apply them all.
return this.filterDelegate.getEnabledFilters(options.contextLevel, options.instanceId);
}
return this.filterProvider.getAvailableInContext(options.contextLevel, options.instanceId, site.getId());
}).catch(() => {
return [];
}).then((filters) => {
const promises = [];
// Apply formatText to title and message.
promises.push(this.filterProvider.formatText(notification.title, options, filters, site.getId())
.then((title) => {
localNotif.title = title;
}).catch(() => {
localNotif.title = notification.title;
}));
promises.push(this.filterProvider.formatText(notification.message, options, filters, site.getId())
.catch(() => {
// Error formatting, use the original message.
return notification.message;
}).then((formattedMessage) => {
if (extraFeatures && isAndroid && this.utils.isFalseOrZero(data.notif)) {
// It's a message, use messaging style. Ionic Native doesn't specify this option.
(<any> localNotif).text = [
{
message: formattedMessage,
message: notification.message,
person: data.conversationtype == 2 ? data.userfromfullname : ''
}
];
} else {
localNotif.text = formattedMessage;
}
}));
if (extraFeatures && isAndroid) {
// Use a different icon if needed.
@ -536,12 +511,8 @@ export class CorePushNotificationsProvider {
}
}
Promise.all(promises).then(() => {
this.localNotificationsProvider.schedule(localNotif, CorePushNotificationsProvider.COMPONENT, data.site,
this.localNotificationsProvider.schedule(localNotif, CorePushNotificationsProvider.COMPONENT, data.site || '',
true);
});
});
}
// Trigger a notification received event.

View File

@ -1306,6 +1306,24 @@ export class CoreSitesProvider {
}
}
/**
* Finds a site with a certain URL. It will return the first site found.
*
* @param siteUrl The site URL.
* @return Promise resolved with the site.
*/
async getSiteByUrl(siteUrl: string): Promise<CoreSite> {
await this.dbReady;
const data = await this.appDB.getRecord(CoreSitesProvider.SITES_TABLE, { siteUrl });
if (typeof this.sites[data.id] != 'undefined') {
return this.sites[data.id];
}
return this.makeSiteFromSiteListEntry(data);
}
/**
* Create a site from an entry of the sites list DB. The new site is added to the list of "cached" sites: this.sites.
*