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 { onMessageReceived(notification: any): void {
const data = notification ? notification.additionalData : {}; 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') { if (typeof data.customdata == 'string') {
data.customdata = this.textUtils.parseJSON(data.customdata, {}); data.customdata = this.textUtils.parseJSON(data.customdata, {});
@ -468,80 +479,40 @@ export class CorePushNotificationsProvider {
// If the app is in foreground when the notification is received, it's not shown. Let's show it ourselves. // If the app is in foreground when the notification is received, it's not shown. Let's show it ourselves.
if (this.localNotificationsProvider.isAvailable()) { if (this.localNotificationsProvider.isAvailable()) {
const localNotif: ILocalNotification = { const localNotif: ILocalNotification = {
id: data.notId || 1, id: data.notId || 1,
data: data, data: data,
title: '', title: notification.title,
text: '', text: notification.message,
channel: 'PushPluginChannel' channel: 'PushPluginChannel'
}, };
options = { const isAndroid = CoreApp.instance.isAndroid();
clean: true, const extraFeatures = this.utils.isTrueOrOne(data.extrafeatures);
singleLine: true,
contextLevel: 'system',
instanceId: 0,
filter: true
},
isAndroid = CoreApp.instance.isAndroid(),
extraFeatures = this.utils.isTrueOrOne(data.extrafeatures);
// Get the filters to apply to text and message. Don't use FIlterHelper to prevent circular dependencies. if (extraFeatures && isAndroid && this.utils.isFalseOrZero(data.notif)) {
this.filterProvider.canGetFilters(site.getId()).then((canGet) => { // It's a message, use messaging style. Ionic Native doesn't specify this option.
if (!canGet) { (<any> localNotif).text = [
// We cannot check which filters are available, apply them all. {
return this.filterDelegate.getEnabledFilters(options.contextLevel, options.instanceId); message: notification.message,
} person: data.conversationtype == 2 ? data.userfromfullname : ''
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,
person: data.conversationtype == 2 ? data.userfromfullname : ''
}
];
} else {
localNotif.text = formattedMessage;
} }
})); ];
}
if (extraFeatures && isAndroid) { if (extraFeatures && isAndroid) {
// Use a different icon if needed. // Use a different icon if needed.
localNotif.icon = notification.image; localNotif.icon = notification.image;
// This feature isn't supported by the official plugin, we use a fork. // This feature isn't supported by the official plugin, we use a fork.
(<any> localNotif).iconType = data['image-type']; (<any> localNotif).iconType = data['image-type'];
localNotif.summary = data.summaryText; localNotif.summary = data.summaryText;
if (data.picture) { if (data.picture) {
localNotif.attachments = [data.picture]; localNotif.attachments = [data.picture];
}
} }
}
Promise.all(promises).then(() => { this.localNotificationsProvider.schedule(localNotif, CorePushNotificationsProvider.COMPONENT, data.site || '',
this.localNotificationsProvider.schedule(localNotif, CorePushNotificationsProvider.COMPONENT, data.site, true);
true);
});
});
} }
// Trigger a notification received event. // 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. * Create a site from an entry of the sites list DB. The new site is added to the list of "cached" sites: this.sites.
* *