MOBILE-2814 localnotif: Adapt the code to the new API
parent
0a13f9890d
commit
3f84adca9a
|
@ -13,7 +13,6 @@
|
|||
// limitations under the License.
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
import { CoreLoggerProvider } from '@providers/logger';
|
||||
import { CoreSitesProvider } from '@providers/sites';
|
||||
import { CoreSite } from '@classes/site';
|
||||
|
@ -23,6 +22,7 @@ import { CoreGroupsProvider } from '@providers/groups';
|
|||
import { CoreConstants } from '@core/constants';
|
||||
import { CoreLocalNotificationsProvider } from '@providers/local-notifications';
|
||||
import { CoreConfigProvider } from '@providers/config';
|
||||
import { ILocalNotification } from '@ionic-native/local-notifications';
|
||||
|
||||
/**
|
||||
* Service to handle calendar events.
|
||||
|
@ -132,8 +132,7 @@ export class AddonCalendarProvider {
|
|||
|
||||
constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private groupsProvider: CoreGroupsProvider,
|
||||
private coursesProvider: CoreCoursesProvider, private timeUtils: CoreTimeUtilsProvider,
|
||||
private localNotificationsProvider: CoreLocalNotificationsProvider, private configProvider: CoreConfigProvider,
|
||||
private translate: TranslateService) {
|
||||
private localNotificationsProvider: CoreLocalNotificationsProvider, private configProvider: CoreConfigProvider) {
|
||||
this.logger = logger.getInstance('AddonCalendarProvider');
|
||||
this.sitesProvider.createTablesFromSchema(this.tablesSchema);
|
||||
}
|
||||
|
@ -526,16 +525,12 @@ export class AddonCalendarProvider {
|
|||
return Promise.resolve();
|
||||
}
|
||||
|
||||
const dateTriggered = new Date((event.timestart - (time * 60)) * 1000),
|
||||
notification = {
|
||||
const notification: ILocalNotification = {
|
||||
id: event.id,
|
||||
title: event.name,
|
||||
text: this.timeUtils.userDate(event.timestart * 1000, 'core.strftimedaydatetime', true),
|
||||
at: dateTriggered,
|
||||
channelParams: {
|
||||
channelID: 'notifications',
|
||||
channelName: this.translate.instant('addon.notifications.notifications'),
|
||||
importance: 4 // IMPORTANCE_HIGH
|
||||
trigger: {
|
||||
at: new Date((event.timestart - (time * 60)) * 1000)
|
||||
},
|
||||
data: {
|
||||
eventid: event.id,
|
||||
|
|
|
@ -13,7 +13,6 @@
|
|||
// limitations under the License.
|
||||
|
||||
import { Injectable, NgZone } from '@angular/core';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
import { Badge } from '@ionic-native/badge';
|
||||
import { Push, PushObject, PushOptions } from '@ionic-native/push';
|
||||
import { Device } from '@ionic-native/device';
|
||||
|
@ -28,6 +27,7 @@ import { CoreTextUtilsProvider } from '@providers/utils/text';
|
|||
import { CoreConfigProvider } from '@providers/config';
|
||||
import { CoreConstants } from '@core/constants';
|
||||
import { CoreConfigConstants } from '../../../configconstants';
|
||||
import { ILocalNotification } from '@ionic-native/local-notifications';
|
||||
|
||||
/**
|
||||
* Service to handle push notifications.
|
||||
|
@ -66,8 +66,7 @@ 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 translate: TranslateService) {
|
||||
private configProvider: CoreConfigProvider, private device: Device, private zone: NgZone) {
|
||||
this.logger = logger.getInstance('AddonPushNotificationsProvider');
|
||||
this.appDB = appProvider.getDB();
|
||||
this.appDB.createTablesFromSchema(this.tablesSchema);
|
||||
|
@ -153,13 +152,10 @@ export class AddonPushNotificationsProvider {
|
|||
if (this.utils.isTrueOrOne(data.foreground)) {
|
||||
// If the app is in foreground when the notification is received, it's not shown. Let's show it ourselves.
|
||||
if (this.localNotificationsProvider.isAvailable()) {
|
||||
const localNotif = {
|
||||
const localNotif: ILocalNotification = {
|
||||
id: 1,
|
||||
at: new Date(),
|
||||
channelParams: {
|
||||
channelID: 'notifications',
|
||||
channelName: this.translate.instant('addon.notifications.notifications'),
|
||||
importance: 4 // IMPORTANCE_HIGH
|
||||
trigger: {
|
||||
at: new Date()
|
||||
},
|
||||
data: {
|
||||
notif: data.notif,
|
||||
|
|
|
@ -24,24 +24,7 @@ import { CoreTextUtilsProvider } from './utils/text';
|
|||
import { CoreUtilsProvider } from './utils/utils';
|
||||
import { SQLiteDB } from '@classes/sqlitedb';
|
||||
import { CoreConstants } from '@core/constants';
|
||||
import { Subject } from 'rxjs';
|
||||
|
||||
/**
|
||||
* Local notification.
|
||||
*/
|
||||
export interface CoreILocalNotification extends ILocalNotification {
|
||||
/**
|
||||
* Number of milliseconds to turn the led on (Android only).
|
||||
* @type {number}
|
||||
*/
|
||||
ledOnTime?: number;
|
||||
|
||||
/**
|
||||
* Number of milliseconds to turn the led off (Android only).
|
||||
* @type {number}
|
||||
*/
|
||||
ledOffTime?: number;
|
||||
}
|
||||
import { Subject, Subscription } from 'rxjs';
|
||||
|
||||
/*
|
||||
* Generated class for the LocalNotificationsProvider provider.
|
||||
|
@ -115,6 +98,8 @@ export class CoreLocalNotificationsProvider {
|
|||
ids: [],
|
||||
timeouts: []
|
||||
};
|
||||
protected triggerSubscription: Subscription;
|
||||
protected clickSubscription: Subscription;
|
||||
|
||||
constructor(logger: CoreLoggerProvider, private localNotifications: LocalNotifications, private platform: Platform,
|
||||
private appProvider: CoreAppProvider, private utils: CoreUtilsProvider, private configProvider: CoreConfigProvider,
|
||||
|
@ -126,16 +111,15 @@ export class CoreLocalNotificationsProvider {
|
|||
this.appDB.createTablesFromSchema(this.tablesSchema);
|
||||
|
||||
platform.ready().then(() => {
|
||||
localNotifications.on('trigger', (notification, state) => {
|
||||
this.triggerSubscription = localNotifications.on('trigger').subscribe((notification: ILocalNotification) => {
|
||||
this.trigger(notification);
|
||||
});
|
||||
|
||||
localNotifications.on('click', (notification, state) => {
|
||||
this.clickSubscription = localNotifications.on('click').subscribe((notification: ILocalNotification) => {
|
||||
if (notification && notification.data) {
|
||||
this.logger.debug('Notification clicked: ', notification.data);
|
||||
|
||||
const data = textUtils.parseJSON(notification.data);
|
||||
this.notifyClick(data);
|
||||
this.notifyClick(notification.data);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -175,7 +159,7 @@ export class CoreLocalNotificationsProvider {
|
|||
return Promise.reject(null);
|
||||
}
|
||||
|
||||
return this.localNotifications.getAllScheduled().then((scheduled) => {
|
||||
return this.localNotifications.getScheduled().then((scheduled) => {
|
||||
const ids = [];
|
||||
|
||||
scheduled.forEach((notif) => {
|
||||
|
@ -291,12 +275,18 @@ export class CoreLocalNotificationsProvider {
|
|||
/**
|
||||
* Check if a notification has been triggered with the same trigger time.
|
||||
*
|
||||
* @param {CoreILocalNotification} notification Notification to check.
|
||||
* @param {ILocalNotification} notification Notification to check.
|
||||
* @return {Promise<any>} Promise resolved with a boolean indicating if promise is triggered (true) or not.
|
||||
*/
|
||||
isTriggered(notification: CoreILocalNotification): Promise<any> {
|
||||
isTriggered(notification: ILocalNotification): Promise<any> {
|
||||
return this.appDB.getRecord(this.TRIGGERED_TABLE, { id: notification.id }).then((stored) => {
|
||||
return stored.at === notification.at.getTime() / 1000;
|
||||
let triggered = (notification.trigger && notification.trigger.at) || 0;
|
||||
|
||||
if (typeof triggered != 'number') {
|
||||
triggered = triggered.getTime();
|
||||
}
|
||||
|
||||
return stored.at === triggered;
|
||||
}).catch(() => {
|
||||
return this.localNotifications.isTriggered(notification.id);
|
||||
});
|
||||
|
@ -426,12 +416,11 @@ export class CoreLocalNotificationsProvider {
|
|||
*/
|
||||
rescheduleAll(): Promise<any> {
|
||||
// Get all the scheduled notifications.
|
||||
return this.localNotifications.getAllScheduled().then((notifications) => {
|
||||
return this.localNotifications.getScheduled().then((notifications) => {
|
||||
const promises = [];
|
||||
|
||||
notifications.forEach((notification) => {
|
||||
// Convert some properties to the needed types.
|
||||
notification.at = new Date(notification.at * 1000);
|
||||
notification.data = notification.data ? this.textUtils.parseJSON(notification.data, {}) : {};
|
||||
|
||||
promises.push(this.scheduleNotification(notification));
|
||||
|
@ -444,13 +433,13 @@ export class CoreLocalNotificationsProvider {
|
|||
/**
|
||||
* Schedule a local notification.
|
||||
*
|
||||
* @param {CoreILocalNotification} notification Notification to schedule. Its ID should be lower than 10000000 and it should
|
||||
* be unique inside its component and site.
|
||||
* @param {ILocalNotification} notification Notification to schedule. Its ID should be lower than 10000000 and it should
|
||||
* be unique inside its component and site.
|
||||
* @param {string} component Component triggering the notification. It is used to generate unique IDs.
|
||||
* @param {string} siteId Site ID.
|
||||
* @return {Promise<any>} Promise resolved when the notification is scheduled.
|
||||
*/
|
||||
schedule(notification: CoreILocalNotification, component: string, siteId: string): Promise<any> {
|
||||
schedule(notification: ILocalNotification, component: string, siteId: string): Promise<any> {
|
||||
return this.getUniqueNotificationId(notification.id, component, siteId).then((uniqueId) => {
|
||||
notification.id = uniqueId;
|
||||
notification.data = notification.data || {};
|
||||
|
@ -460,9 +449,13 @@ export class CoreLocalNotificationsProvider {
|
|||
if (this.platform.is('android')) {
|
||||
notification.icon = notification.icon || 'res://icon';
|
||||
notification.smallIcon = notification.smallIcon || 'res://icon';
|
||||
notification.led = notification.led || 'FF9900';
|
||||
notification.ledOnTime = notification.ledOnTime || 1000;
|
||||
notification.ledOffTime = notification.ledOffTime || 1000;
|
||||
|
||||
const led: any = notification.led || {};
|
||||
notification.led = {
|
||||
color: led.color || 'FF9900',
|
||||
on: led.on || 1000,
|
||||
off: led.off || 1000
|
||||
};
|
||||
}
|
||||
|
||||
return this.scheduleNotification(notification);
|
||||
|
@ -472,10 +465,10 @@ export class CoreLocalNotificationsProvider {
|
|||
/**
|
||||
* Helper function to schedule a notification object if it hasn't been triggered already.
|
||||
*
|
||||
* @param {CoreILocalNotification} notification Notification to schedule.
|
||||
* @param {ILocalNotification} notification Notification to schedule.
|
||||
* @return {Promise<any>} Promise resolved when scheduled.
|
||||
*/
|
||||
protected scheduleNotification(notification: CoreILocalNotification): Promise<any> {
|
||||
protected scheduleNotification(notification: ILocalNotification): Promise<any> {
|
||||
// Check if the notification has been triggered already.
|
||||
return this.isTriggered(notification).then((triggered) => {
|
||||
// Cancel the current notification in case it gets scheduled twice.
|
||||
|
@ -503,9 +496,9 @@ export class CoreLocalNotificationsProvider {
|
|||
* This function was used because local notifications weren't displayed when the app was in foreground in iOS10+,
|
||||
* but the issue was fixed in the plugin and this function is no longer used.
|
||||
*
|
||||
* @param {CoreILocalNotification} notification Notification.
|
||||
* @param {ILocalNotification} notification Notification.
|
||||
*/
|
||||
showNotificationPopover(notification: CoreILocalNotification): void {
|
||||
showNotificationPopover(notification: ILocalNotification): void {
|
||||
|
||||
if (!notification || !notification.title || !notification.text) {
|
||||
// Invalid data.
|
||||
|
@ -595,13 +588,13 @@ export class CoreLocalNotificationsProvider {
|
|||
* Function to call when a notification is triggered. Stores the notification so it's not scheduled again unless the
|
||||
* time is changed.
|
||||
*
|
||||
* @param {CoreILocalNotification} notification Triggered notification.
|
||||
* @param {ILocalNotification} notification Triggered notification.
|
||||
* @return {Promise<any>} Promise resolved when stored, rejected otherwise.
|
||||
*/
|
||||
trigger(notification: CoreILocalNotification): Promise<any> {
|
||||
trigger(notification: ILocalNotification): Promise<any> {
|
||||
const entry = {
|
||||
id: notification.id,
|
||||
at: parseInt(notification.at, 10)
|
||||
at: notification.trigger && notification.trigger.at ? notification.trigger.at : Date.now()
|
||||
};
|
||||
|
||||
return this.appDB.insertRecord(this.TRIGGERED_TABLE, entry);
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
This files describes API changes in the Moodle Mobile app,
|
||||
information provided here is intended especially for developers.
|
||||
|
||||
=== 3.6.1 ===
|
||||
|
||||
- The local notifications plugin was updated to its latest version. The new API has some breaking changes, so please check its documentation if you're using local notifications. Also, you need to run "npm install" to update the ionic-native library.
|
||||
|
||||
=== 3.6.0 ===
|
||||
|
||||
- gulp was updated to v4. In order for gulp to work, you need to install gulp-cli: npm install -g gulp-cli
|
||||
|
|
Loading…
Reference in New Issue