MOBILE-2831 push: Handle notification channels

main
Dani Palou 2019-02-01 15:35:13 +01:00
parent 30f8efd14a
commit cca7dee8eb
6 changed files with 65 additions and 12 deletions

View File

@ -68,6 +68,7 @@
"addon.blog.siteblogheading": "blog",
"addon.calendar.calendar": "calendar",
"addon.calendar.calendarevents": "local_moodlemobileapp",
"addon.calendar.calendarreminders": "local_moodlemobileapp",
"addon.calendar.defaultnotificationtime": "local_moodlemobileapp",
"addon.calendar.errorloadevent": "local_moodlemobileapp",
"addon.calendar.errorloadevents": "local_moodlemobileapp",
@ -1506,6 +1507,7 @@
"core.maxsizeandattachments": "moodle",
"core.min": "moodle",
"core.mins": "moodle",
"core.misc": "admin",
"core.mod_assign": "assign/pluginname",
"core.mod_assignment": "assignment/pluginname",
"core.mod_book": "book/pluginname",

View File

@ -1,6 +1,7 @@
{
"calendar": "Calendar",
"calendarevents": "Calendar events",
"calendarreminders": "Calendar reminders",
"defaultnotificationtime": "Default notification time",
"errorloadevent": "Error loading event.",
"errorloadevents": "Error loading events.",

View File

@ -13,9 +13,11 @@
// limitations under the License.
import { Injectable, NgZone } from '@angular/core';
import { Platform } from 'ionic-angular';
import { Badge } from '@ionic-native/badge';
import { Push, PushObject, PushOptions } from '@ionic-native/push';
import { Device } from '@ionic-native/device';
import { TranslateService } from '@ngx-translate/core';
import { CoreAppProvider } from '@providers/app';
import { CoreInitDelegate } from '@providers/init';
import { CoreLoggerProvider } from '@providers/logger';
@ -67,10 +69,21 @@ export class AddonPushNotificationsProvider {
protected pushNotificationsDelegate: AddonPushNotificationsDelegate, protected sitesProvider: CoreSitesProvider,
private badge: Badge, private localNotificationsProvider: CoreLocalNotificationsProvider,
private utils: CoreUtilsProvider, private textUtils: CoreTextUtilsProvider, private push: Push,
private configProvider: CoreConfigProvider, private device: Device, private zone: NgZone) {
private configProvider: CoreConfigProvider, private device: Device, private zone: NgZone,
private translate: TranslateService, private platform: Platform) {
this.logger = logger.getInstance('AddonPushNotificationsProvider');
this.appDB = appProvider.getDB();
this.appDB.createTablesFromSchema(this.tablesSchema);
platform.ready().then(() => {
// Create the default channel.
this.createDefaultChannel();
translate.onLangChange.subscribe((event: any) => {
// Update the channel name.
this.createDefaultChannel();
});
});
}
/**
@ -85,6 +98,25 @@ export class AddonPushNotificationsProvider {
});
}
/**
* Create the default push channel. It is used to change the name.
*
* @return {Promise<any>} Promise resolved when done.
*/
protected createDefaultChannel(): Promise<any> {
if (!this.platform.is('android')) {
return Promise.resolve();
}
return this.push.createChannel({
id: 'PushPluginChannel',
description: this.translate.instant('core.misc'),
importance: 4
}).catch((error) => {
this.logger.error('Error changing push channel name', error);
});
}
/**
* Returns options for push notifications based on device.
*
@ -155,13 +187,7 @@ export class AddonPushNotificationsProvider {
if (this.localNotificationsProvider.isAvailable()) {
const localNotif: ILocalNotification = {
id: 1,
trigger: {
at: new Date()
},
data: {
notif: data.notif,
site: data.site
},
data: data,
title: '',
text: ''
},
@ -193,9 +219,6 @@ export class AddonPushNotificationsProvider {
});
} else {
// The notification was clicked.
// For compatibility with old push plugin implementation we'll merge all the notification data in a single object.
data.title = notification.title;
data.message = notification.message;
this.notificationClicked(data);
}
});

View File

@ -68,6 +68,7 @@
"addon.blog.siteblogheading": "Site blog",
"addon.calendar.calendar": "Calendar",
"addon.calendar.calendarevents": "Calendar events",
"addon.calendar.calendarreminders": "Calendar reminders",
"addon.calendar.defaultnotificationtime": "Default notification time",
"addon.calendar.errorloadevent": "Error loading event.",
"addon.calendar.errorloadevents": "Error loading events.",
@ -1506,6 +1507,7 @@
"core.maxsizeandattachments": "Maximum size for new files: {{$a.size}}, maximum attachments: {{$a.attachments}}",
"core.min": "min",
"core.mins": "mins",
"core.misc": "Miscellaneous",
"core.mod_assign": "Assignment",
"core.mod_assignment": "Assignment 2.2 (Disabled)",
"core.mod_book": "Book",

View File

@ -126,6 +126,7 @@
"maxsizeandattachments": "Maximum size for new files: {{$a.size}}, maximum attachments: {{$a.attachments}}",
"min": "min",
"mins": "mins",
"misc": "Miscellaneous",
"mod_assign": "Assignment",
"mod_assignment": "Assignment 2.2 (Disabled)",
"mod_book": "Book",

View File

@ -15,6 +15,7 @@
import { Injectable } from '@angular/core';
import { Platform, Alert, AlertController } from 'ionic-angular';
import { LocalNotifications, ILocalNotification } from '@ionic-native/local-notifications';
import { Push } from '@ionic-native/push';
import { TranslateService } from '@ngx-translate/core';
import { CoreAppProvider } from './app';
import { CoreConfigProvider } from './config';
@ -104,7 +105,7 @@ export class CoreLocalNotificationsProvider {
constructor(logger: CoreLoggerProvider, private localNotifications: LocalNotifications, private platform: Platform,
private appProvider: CoreAppProvider, private utils: CoreUtilsProvider, private configProvider: CoreConfigProvider,
private textUtils: CoreTextUtilsProvider, private translate: TranslateService, private alertCtrl: AlertController,
eventsProvider: CoreEventsProvider) {
eventsProvider: CoreEventsProvider, private push: Push) {
this.logger = logger.getInstance('CoreLocalNotificationsProvider');
this.appDB = appProvider.getDB();
@ -122,6 +123,14 @@ export class CoreLocalNotificationsProvider {
this.notifyClick(notification.data);
}
});
// Create the default channel for local notifications.
this.createDefaultChannel();
translate.onLangChange.subscribe((event: any) => {
// Update the channel name.
this.createDefaultChannel();
});
});
eventsProvider.on(CoreEventsProvider.SITE_DELETED, (site) => {
@ -176,6 +185,21 @@ export class CoreLocalNotificationsProvider {
});
}
/**
* Create the default channel. It is used to change the name.
*
* @return {Promise<any>} Promise resolved when done.
*/
protected createDefaultChannel(): Promise<any> {
return this.push.createChannel({
id: 'default-channel-id',
description: this.translate.instant('addon.calendar.calendarreminders'),
importance: 4
}).catch((error) => {
this.logger.error('Error changing channel name', error);
});
}
/**
* Get a code to create unique notifications. If there's no code assigned, create a new one.
*