function checkNotificationPermissions()
{
	if (!("Notification" in window)) 
	{
		console.log("This browser does not support desktop notification");
	}
	else
	{
		if (window.Notification.permission !== "granted")
		{
			//console.info('Notication permission denied - requesting permission...');
			/**
			Promise.resolve(window.Notification.requestPermission()).then
			(
				function(permission)
				{
					alert('requested permission');
					console.log(permission); 
					processPendingNotifications();
				}
			);
			**/
			try 
			{
				//alert('Requesting permissions...');
				Notification.requestPermission().then
				(() => 
					{
						//alert(">>"+window.Notification);
						processPendingNotifications();
					}
				);
			} 
			catch (error) 
			{
				// Safari doesn't return a promise for requestPermissions and it
				// throws a TypeError. It takes a callback as the first argument
				// instead.
				console.error(error);
				if (error instanceof TypeError)
				{
					Notification.requestPermission
					(() => 
						{
							processPendingNotifications();
						}
					);
				} 
				else 
				{
					throw error;
				}
			}		
		}
		else 
		{
			//console.log('Notification permissions allowed.');
			processPendingNotifications();
		}
	}
};

function processPendingNotifications()
{
	//console.info('Processing push notification with ID '+sablePushID);
	$.ajax({
				url : "/lib/get_push_notifications.php",
				type: "POST",
				data: {'sablePushID': sablePushID},
				success: function(data, textStatus, jqXHR) 
				{
					var jData = jQuery.parseJSON(data);
					var good = (parseInt(jData.result) > 0) ? true : false;
					if (good)
					{
						//console.log('inside push notification...');
						for (var i = jData.notif.length - 1; i >= 0; i--)
						{
							var opts = 
							{
								body: jData.notif[i].msg, 
								icon: jData.notif[i].icon,
								badge: jData.notif[i].badge, 
								vibrate:[100,50,100], 
								silent:false,
								data: { url:jData.notif[i].url},
								actions: [{action: "open_url", title: "Open Now"}],									
							};
							runNotification(jData.notif[i].title, opts);
						}						
					}
				},
				error: function(jqXHR, textStatus, errorThrown){}
			});	
}

function runNotification(title, options)
{
	navigator.serviceWorker.ready.then(function(registration){registration.showNotification(title, options);});
}