forked from CIT/Vmeda.Online
		
	MOBILE-3633 notifications: Implement cron and push handlers
This commit is contained in:
		
							parent
							
								
									0d31e76daa
								
							
						
					
					
						commit
						11381458ad
					
				@ -15,9 +15,13 @@
 | 
				
			|||||||
import { APP_INITIALIZER, NgModule } from '@angular/core';
 | 
					import { APP_INITIALIZER, NgModule } from '@angular/core';
 | 
				
			||||||
import { Routes } from '@angular/router';
 | 
					import { Routes } from '@angular/router';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { CoreCronDelegate } from '@services/cron';
 | 
				
			||||||
import { CoreMainMenuDelegate } from '@features/mainmenu/services/mainmenu-delegate';
 | 
					import { CoreMainMenuDelegate } from '@features/mainmenu/services/mainmenu-delegate';
 | 
				
			||||||
import { CoreMainMenuRoutingModule } from '@features/mainmenu/mainmenu-routing.module';
 | 
					import { CoreMainMenuRoutingModule } from '@features/mainmenu/mainmenu-routing.module';
 | 
				
			||||||
 | 
					import { CorePushNotificationsDelegate } from '@features/pushnotifications/services/push-delegate';
 | 
				
			||||||
import { AddonNotificationsMainMenuHandler, AddonNotificationsMainMenuHandlerService } from './services/handlers/mainmenu';
 | 
					import { AddonNotificationsMainMenuHandler, AddonNotificationsMainMenuHandlerService } from './services/handlers/mainmenu';
 | 
				
			||||||
 | 
					import { AddonNotificationsCronHandler } from './services/handlers/cron';
 | 
				
			||||||
 | 
					import { AddonNotificationsPushClickHandler } from './services/handlers/push-click';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const routes: Routes = [
 | 
					const routes: Routes = [
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
@ -36,6 +40,8 @@ const routes: Routes = [
 | 
				
			|||||||
            deps: [],
 | 
					            deps: [],
 | 
				
			||||||
            useFactory: () => () => {
 | 
					            useFactory: () => () => {
 | 
				
			||||||
                CoreMainMenuDelegate.instance.registerHandler(AddonNotificationsMainMenuHandler.instance);
 | 
					                CoreMainMenuDelegate.instance.registerHandler(AddonNotificationsMainMenuHandler.instance);
 | 
				
			||||||
 | 
					                CoreCronDelegate.instance.register(AddonNotificationsCronHandler.instance);
 | 
				
			||||||
 | 
					                CorePushNotificationsDelegate.instance.registerClickHandler(AddonNotificationsPushClickHandler.instance);
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
    ],
 | 
					    ],
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										80
									
								
								src/addons/notifications/services/handlers/cron.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								src/addons/notifications/services/handlers/cron.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,80 @@
 | 
				
			|||||||
 | 
					// (C) Copyright 2015 Moodle Pty Ltd.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// Licensed under the Apache License, Version 2.0 (the "License");
 | 
				
			||||||
 | 
					// you may not use this file except in compliance with the License.
 | 
				
			||||||
 | 
					// You may obtain a copy of the License at
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//     http://www.apache.org/licenses/LICENSE-2.0
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// Unless required by applicable law or agreed to in writing, software
 | 
				
			||||||
 | 
					// distributed under the License is distributed on an "AS IS" BASIS,
 | 
				
			||||||
 | 
					// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
				
			||||||
 | 
					// See the License for the specific language governing permissions and
 | 
				
			||||||
 | 
					// limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { Injectable } from '@angular/core';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { CoreApp } from '@services/app';
 | 
				
			||||||
 | 
					import { CoreCronHandler } from '@services/cron';
 | 
				
			||||||
 | 
					import { CoreSites } from '@services/sites';
 | 
				
			||||||
 | 
					import { makeSingleton } from '@singletons';
 | 
				
			||||||
 | 
					import { CoreEvents } from '@singletons/events';
 | 
				
			||||||
 | 
					import { AddonNotifications, AddonNotificationsProvider } from '../notifications';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Notifications cron handler.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					@Injectable()
 | 
				
			||||||
 | 
					export class AddonNotificationsCronHandlerService implements CoreCronHandler {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    name = 'AddonNotificationsCronHandler';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Get the time between consecutive executions.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @return Time between consecutive executions (in ms).
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    getInterval(): number {
 | 
				
			||||||
 | 
					        return CoreApp.instance.isMobile() ? 600000 : 60000; // 1 or 10 minutes.
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Check whether it's a synchronization process or not. True if not defined.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @return Whether it's a synchronization process or not.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    isSync(): boolean {
 | 
				
			||||||
 | 
					        // This is done to use only wifi if using the fallback function.
 | 
				
			||||||
 | 
					        return !AddonNotifications.instance.isPreciseNotificationCountEnabled();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Check whether the sync can be executed manually. Call isSync if not defined.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @return Whether the sync can be executed manually.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    canManualSync(): boolean {
 | 
				
			||||||
 | 
					        return true;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Execute the process.
 | 
				
			||||||
 | 
					     * Receives the ID of the site affected, undefined for all sites.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param siteId ID of the site affected, undefined for all sites.
 | 
				
			||||||
 | 
					     * @param force Wether the execution is forced (manual sync).
 | 
				
			||||||
 | 
					     * @return Promise resolved when done, rejected if failure. If the promise is rejected, this function
 | 
				
			||||||
 | 
					     *         will be called again often, it shouldn't be abused.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    // eslint-disable-next-line @typescript-eslint/no-unused-vars
 | 
				
			||||||
 | 
					    async execute(siteId?: string, force?: boolean): Promise<void> {
 | 
				
			||||||
 | 
					        if (!CoreSites.instance.isCurrentSite(siteId)) {
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        CoreEvents.trigger(AddonNotificationsProvider.READ_CRON_EVENT, {}, CoreSites.instance.getCurrentSiteId());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export class AddonNotificationsCronHandler extends makeSingleton(AddonNotificationsCronHandlerService) {}
 | 
				
			||||||
							
								
								
									
										136
									
								
								src/addons/notifications/services/handlers/push-click.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										136
									
								
								src/addons/notifications/services/handlers/push-click.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,136 @@
 | 
				
			|||||||
 | 
					// (C) Copyright 2015 Moodle Pty Ltd.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// Licensed under the Apache License, Version 2.0 (the "License");
 | 
				
			||||||
 | 
					// you may not use this file except in compliance with the License.
 | 
				
			||||||
 | 
					// You may obtain a copy of the License at
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//     http://www.apache.org/licenses/LICENSE-2.0
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// Unless required by applicable law or agreed to in writing, software
 | 
				
			||||||
 | 
					// distributed under the License is distributed on an "AS IS" BASIS,
 | 
				
			||||||
 | 
					// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
				
			||||||
 | 
					// See the License for the specific language governing permissions and
 | 
				
			||||||
 | 
					// limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { Injectable } from '@angular/core';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { CoreNavHelper } from '@services/nav-helper';
 | 
				
			||||||
 | 
					import { CoreTextUtils } from '@services/utils/text';
 | 
				
			||||||
 | 
					import { CoreUtils } from '@services/utils/utils';
 | 
				
			||||||
 | 
					import { makeSingleton } from '@singletons';
 | 
				
			||||||
 | 
					import { CoreEvents } from '@singletons/events';
 | 
				
			||||||
 | 
					import { CorePushNotificationsClickHandler } from '@features/pushnotifications/services/push-delegate';
 | 
				
			||||||
 | 
					import { CorePushNotificationsNotificationBasicData } from '@features/pushnotifications/services/pushnotifications';
 | 
				
			||||||
 | 
					import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper';
 | 
				
			||||||
 | 
					import { AddonNotifications, AddonNotificationsProvider } from '../notifications';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Handler for non-messaging push notifications clicks.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					@Injectable()
 | 
				
			||||||
 | 
					export class AddonNotificationsPushClickHandlerService implements CorePushNotificationsClickHandler {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    name = 'AddonNotificationsPushClickHandler';
 | 
				
			||||||
 | 
					    priority = 0; // Low priority so it's used as a fallback if no other handler treats the notification.
 | 
				
			||||||
 | 
					    featureName = 'CoreMainMenuDelegate_AddonNotifications';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Check if a notification click is handled by this handler.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param notification The notification to check.
 | 
				
			||||||
 | 
					     * @return Whether the notification click is handled by this handler
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    async handles(notification: NotificationData): Promise<boolean> {
 | 
				
			||||||
 | 
					        if (!notification.moodlecomponent) {
 | 
				
			||||||
 | 
					            // The notification doesn't come from Moodle. Handle it.
 | 
				
			||||||
 | 
					            return true;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (CoreUtils.instance.isTrueOrOne(notification.notif)) {
 | 
				
			||||||
 | 
					            // Notification clicked, mark as read. Don't block for this.
 | 
				
			||||||
 | 
					            this.markAsRead(notification);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return true;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Mark the notification as read.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param notification Notification to mark.
 | 
				
			||||||
 | 
					     * @return Promise resolved when done.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    protected async markAsRead(notification: NotificationData): Promise<void> {
 | 
				
			||||||
 | 
					        const notifId = notification.savedmessageid || notification.id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!notifId) {
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        await CoreUtils.instance.ignoreErrors(AddonNotifications.instance.markNotificationRead(notifId, notification.site));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        CoreEvents.trigger(AddonNotificationsProvider.READ_CHANGED_EVENT, {}, notification.site);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Handle the notification click.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param notification The notification to check.
 | 
				
			||||||
 | 
					     * @return Promise resolved when done.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    async handleClick(notification: NotificationData): Promise<void> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (notification.customdata?.extendedtext) {
 | 
				
			||||||
 | 
					            // Display the text in a modal.
 | 
				
			||||||
 | 
					            return CoreTextUtils.instance.viewText(notification.title || '', <string> notification.customdata.extendedtext, {
 | 
				
			||||||
 | 
					                displayCopyButton: true,
 | 
				
			||||||
 | 
					                // @todo modalOptions: { cssClass: 'core-modal-fullscreen' },
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Try to handle the appurl.
 | 
				
			||||||
 | 
					        if (notification.customdata?.appurl) {
 | 
				
			||||||
 | 
					            const url = <string> notification.customdata.appurl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            switch (notification.customdata.appurlopenin) {
 | 
				
			||||||
 | 
					                case 'inapp':
 | 
				
			||||||
 | 
					                    CoreUtils.instance.openInApp(url);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                case 'browser':
 | 
				
			||||||
 | 
					                    return CoreUtils.instance.openInBrowser(url);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                default:
 | 
				
			||||||
 | 
					                    if (CoreContentLinksHelper.instance.handleLink(url, undefined, undefined, true)) {
 | 
				
			||||||
 | 
					                        // Link treated, stop.
 | 
				
			||||||
 | 
					                        return;
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // No appurl or cannot be handled by the app. Try to handle the contexturl now.
 | 
				
			||||||
 | 
					        if (notification.contexturl) {
 | 
				
			||||||
 | 
					            if (CoreContentLinksHelper.instance.handleLink(notification.contexturl)) {
 | 
				
			||||||
 | 
					                // Link treated, stop.
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // No contexturl or cannot be handled by the app. Open the notifications page.
 | 
				
			||||||
 | 
					        await CoreUtils.instance.ignoreErrors(AddonNotifications.instance.invalidateNotificationsList(notification.site));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        await CoreNavHelper.instance.goInSite('notifications', {}, notification.site);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export class AddonNotificationsPushClickHandler extends makeSingleton(AddonNotificationsPushClickHandlerService) {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type NotificationData = CorePushNotificationsNotificationBasicData & {
 | 
				
			||||||
 | 
					    contexturl?: string; // URL related to the notification.
 | 
				
			||||||
 | 
					    savedmessageid?: number; // Notification ID (optional).
 | 
				
			||||||
 | 
					    id?: number; // Notification ID (optional).
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user