//notify.js PLUGIN - start
(function(root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD environment
define('notify', [], function() {
return factory(root, document);
});
} else {
// Browser environment
root.Notify = factory(root, document);
}
}
(
this,
function(w, d) {
'use strict';
function Notify(title, options) {
this.title = typeof title === 'string' ? title : null;
this.options = {
icon: '',
body: '',
tag: '',
notifyShow: null,
notifyClose: null,
notifyClick: null,
notifyError: null,
permissionGranted: null,
permissionDenied: null,
timeout: null
};
this.permission = null;
if (!Notify.isSupported()) {
return;
}
if (!this.title) {
throw new Error(
'Notify(): first arg (title) must be a string.');
}
// User defined options for notification content
if (typeof options === 'object') {
for (var i in options) {
if (options.hasOwnProperty(i)) {
this.options[i] = options[i];
}
}
// callback when notification is displayed
if (typeof this.options.notifyShow === 'function') {
this.onShowCallback = this.options.notifyShow;
}
// callback when notification is closed
if (typeof this.options.notifyClose === 'function') {
this.onCloseCallback = this.options.notifyClose;
}
// callback when notification is clicked
if (typeof this.options.notifyClick === 'function') {
this.onClickCallback = this.options.notifyClick;
}
// callback when notification throws error
if (typeof this.options.notifyError === 'function') {
this.onErrorCallback = this.options.notifyError;
}
}
}
// return true if the browser supports HTML5 Notification
Notify.isSupported = function() {
if ('Notification' in w) {
return true;
}
return false;
};
// returns true if the permission is not granted
Notify.needsPermission = function() {
if (Notify.isSupported() && Notification.permission === 'granted') {
return false;
}
return true;
};
// asks the user for permission to display notifications.
// Then calls the callback functions is supplied.
Notify.requestPermission = function(
onPermissionGrantedCallback,
onPermissionDeniedCallback) {
if (Notify.isSupported()) {
w.Notification
.requestPermission(function(perm) {
switch (perm) {
case 'granted':
if (typeof onPermissionGrantedCallback === 'function') {
onPermissionGrantedCallback();
}
break;
case 'denied':
/*
* if (typeof
* onPermissionDeniedCallback ===
* 'function') {
* onPermissionDeniedCallback(); }
*/
if (getCookie('notification_error') == undefined) {
var err_div = '
' + '
Web Notifications are disabled
' + '
' + '
To view desktop notifications for latest news from Webdunia, please allow notifications from browser permissions.
' + '
';
if ($('#web_not_error').length == 0)
// $('.mainContainer:first
// #header').prepend(err_div);
$('body').append(err_div);
document.cookie = "notification_error=1; expires=; path=/";
}
break;
}
});
}
};
Notify.prototype.show = function() {
var that = this;
if (!Notify.isSupported()) {
return;
}
this.myNotify = new Notification(this.title, {
'body': this.options.body,
'tag': this.options.tag,
'icon': this.options.icon
});
if (this.options.timeout && !isNaN(this.options.timeout)) {
setTimeout(this.close.bind(this),
this.options.timeout * 1000);
}
this.myNotify.addEventListener('show', this, false);
this.myNotify.addEventListener('error', this, false);
this.myNotify.addEventListener('close', this, false);
this.myNotify.addEventListener('click', this, false);
};
Notify.prototype.onShowNotification = function(e) {
if (this.onShowCallback) {
this.onShowCallback(e);
}
};
Notify.prototype.onCloseNotification = function() {
if (this.onCloseCallback) {
this.onCloseCallback();
}
this.destroy();
};
Notify.prototype.onClickNotification = function() {
if (this.onClickCallback) {
this.onClickCallback();
}
};
Notify.prototype.onErrorNotification = function() {
if (this.onErrorCallback) {
this.onErrorCallback();
}
this.destroy();
};
Notify.prototype.destroy = function() {
this.myNotify.removeEventListener('show', this, false);
this.myNotify.removeEventListener('error', this, false);
this.myNotify.removeEventListener('close', this, false);
this.myNotify.removeEventListener('click', this, false);
};
Notify.prototype.close = function() {
this.myNotify.close();
};
Notify.prototype.handleEvent = function(e) {
switch (e.type) {
case 'show':
this.onShowNotification(e);
break;
case 'close':
this.onCloseNotification(e);
break;
case 'click':
this.onClickNotification(e);
break;
case 'error':
this.onErrorNotification(e);
break;
}
};
return Notify;
}));
// notify.js PLUGIN - end
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i setIntervalTime)){
$.ajax({
type: 'POST',
url: 'https://hindi.webdunia.com/desktop-notifications.php',
// data : { },
dataType: 'json',
success: function(data) {
var status = data['status'];
if (status == 'success') {
var dt = new Date();
dt.setTime(dt.getTime() + (12*60*60*1000));
document.cookie="lastNotificationTime=" + curTime + "; expires=" + dt.toUTCString() + "; path=/";
var maxTimeTopStory = getCookie('maxTimeTopStory');
if(maxTimeTopStory == undefined || isNaN(maxTimeTopStory) || (data['maxTime'] > maxTimeTopStory)){
var j = data.news.length;
var objItem;
for (var i = 0; i < j; i++) {
objItem = data['news'][i];
if((objItem['time'] > maxTimeTopStory) || (maxTimeTopStory == undefined)){
var my_notification = new Notify(
objItem['heading'], {
body: objItem['description'],
tag: "article_" + objItem['article_id'],
icon: 'https://media.webdunia.com/include/_mod/site/theme-5/images/notification_logo.png',
link: objItem['url'],
notifyClick: function() {
window.open(this.options.link);
}
});
my_notification.show();
}
}
document.cookie="maxTimeTopStory=" + data['maxTime'] + "; expires=" + dt.toUTCString() + "; path=/";
}
} else {
// console.log('Error - ' + message);
}
},
error: function(xhr, status, error) {
// console.log(error);
}
});
}
setTimeout(desktopNotification,setIntervalTime);
} else {
console.log("Desktop Notification is not supported");
}
}