mirror of
				https://github.com/pierre42100/ComunicWeb
				synced 2025-11-03 19:54:14 +00:00 
			
		
		
		
	Play a song when new notifications arrives.
This commit is contained in:
		@@ -6,6 +6,11 @@
 | 
			
		||||
 | 
			
		||||
ComunicWeb.components.notifications.service = {
 | 
			
		||||
	
 | 
			
		||||
	/**
 | 
			
		||||
	 * Last known number of notifications
 | 
			
		||||
	 */
 | 
			
		||||
	last_notifs_number: -1,
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Init the service
 | 
			
		||||
	 * 
 | 
			
		||||
@@ -19,17 +24,18 @@ ComunicWeb.components.notifications.service = {
 | 
			
		||||
	init: function(target, auto_hide, target_conversations){
 | 
			
		||||
 | 
			
		||||
		//Initialize interval
 | 
			
		||||
		var interval = setInterval(function(){
 | 
			
		||||
		var interval = setInterval(() => {
 | 
			
		||||
 | 
			
		||||
			//Auto-remove interval if the target has been removed
 | 
			
		||||
			if(!target.isConnected){
 | 
			
		||||
				ComunicWeb.common.pageTitle.setNotificationsNumber(0);
 | 
			
		||||
				this.last_notifs_number = -1;
 | 
			
		||||
				return clearInterval(interval);
 | 
			
		||||
			}
 | 
			
		||||
				
 | 
			
		||||
 | 
			
		||||
			//Get the number of notifications from the API
 | 
			
		||||
			ComunicWeb.components.notifications.interface.getAllUnread(function(response){
 | 
			
		||||
			ComunicWeb.components.notifications.interface.getAllUnread(response => {
 | 
			
		||||
 | 
			
		||||
				//Continue in case of success
 | 
			
		||||
				if(response.error)
 | 
			
		||||
@@ -58,8 +64,17 @@ ComunicWeb.components.notifications.service = {
 | 
			
		||||
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				//Sum notification number
 | 
			
		||||
				let total_number_notifs = response.notifications + response.conversations;
 | 
			
		||||
 | 
			
		||||
				//Update page title too
 | 
			
		||||
				ComunicWeb.common.pageTitle.setNotificationsNumber(response.notifications + response.conversations);
 | 
			
		||||
				ComunicWeb.common.pageTitle.setNotificationsNumber(total_number_notifs);
 | 
			
		||||
 | 
			
		||||
				//Play song if required
 | 
			
		||||
				if(this.last_notifs_number != -1 && total_number_notifs > this.last_notifs_number)
 | 
			
		||||
					ComunicWeb.components.notifications.song.play();
 | 
			
		||||
				
 | 
			
		||||
				this.last_notifs_number = total_number_notifs;
 | 
			
		||||
			});
 | 
			
		||||
 | 
			
		||||
		}, 2000);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										41
									
								
								assets/js/components/notifications/song.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								assets/js/components/notifications/song.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Notification song
 | 
			
		||||
 * 
 | 
			
		||||
 * @author Pierre HUBERT
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
ComunicWeb.components.notifications.song = {
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Song element : null by default
 | 
			
		||||
	 */
 | 
			
		||||
	songElem: null,
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Play notification song once
 | 
			
		||||
	 */
 | 
			
		||||
	play: function(){
 | 
			
		||||
		
 | 
			
		||||
		//Create song element if required
 | 
			
		||||
		if(this.songElem == null){
 | 
			
		||||
			this.songElem = createElem2({
 | 
			
		||||
				type: "audio"
 | 
			
		||||
			});
 | 
			
		||||
 | 
			
		||||
			createElem2({
 | 
			
		||||
				type: "source",
 | 
			
		||||
				appendTo: this.songElem,
 | 
			
		||||
				src: ComunicWeb.__config.assetsURL + "audio/notif_song.mp3"
 | 
			
		||||
			});
 | 
			
		||||
 | 
			
		||||
			createElem2({
 | 
			
		||||
				type: "source",
 | 
			
		||||
				appendTo: this.songElem,
 | 
			
		||||
				src: ComunicWeb.__config.assetsURL + "audio/notif_song.ogg"
 | 
			
		||||
			});
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		//Play song
 | 
			
		||||
		this.songElem.play();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user