Notifications API, showing multiple times - javascript

I'm using the chrome(Mozilla, etc) notifications API:
Notification.requestPermission(function (permission) {
// Whatever the user answers, we make sure we store the information
if (!('permission' in Notification)) {
Notification.permission = permission;
}
// If the user is okay, let's create a notification
if (permission === "granted") {
var options = {
body: data.nome + ': ' + data.corpo,
icon: data.img,
dir : "ltr"
};
var notification = new Notification("Weeazy",options);
}
});
When i'm with 2 or more windows opened, the notification shows multiple times (1 per window).
Someone knows how i can prevent this? I just want to show 1 notification, even with 2, or 999 windows opened, like Facebook does. I hope my code example can help! Thanks!

Related

Bot status changes temporary

I have PresenceUpdate Set so when a specific user streams the bot status updates and it announces in the channel. It did announce and it worked fine but the bot status seems to change for only ~20 seconds then it would go back to its default.
I've tried fixing it using:
For Loop
While Loop
Recursion function
Can anyone help me out and fix this? Here is my code:
client.on("presenceUpdate", (oldPresence, newPresence) => {
let announcement = "Announcement Message";
if (!newPresence.activities) return false;
newPresence.activities.forEach(activity => {
if (activity.type == "STREAMING") {
if(newPresence.user.id == "ID OF USER STREAMING") {
console.log(`${newPresence.user.tag} is streaming at ${activity.url}.`);
client.channels.cache.get("My Channel ID").send(announcement);
client.user.setActivity("to the server", { type: "STREAMING", url: "Twitch Link"});
};
};
});
});
EDIT: Okay so i managed to fix it by removing the part where if they r not streaming the status returns to default. HOWEVER, now it is stuck on streaming even when the user is no longer streaming. How do i solve this? The code has been also updated.
You could store the streaming user's id somewhere, and then check whenever the presence is update if it is this same user.
Here is an example :
if (client.streamingUserID !== undefined && client.streamingUserID !== newPresence.user.id) return;
newPresence.activities.forEach(activity => {
if (activity.type == "STREAMING") {
if(newPresence.user.id == "ID OF USER STREAMING") {
console.log(`${newPresence.user.tag} is streaming at ${activity.url}.`);
client.channels.cache.get("My Channel ID").send(announcement);
client.user.setActivity("to the server", { type: "STREAMING", url: "Twitch Link"});
client.streamingUserID = newPresence.user.id;
};
} else {
// set back activity to default and
delete client.streamingUserID;
}
});

Desktop push notification?

I want to send the desktop notification to my customer dynamical from the server side. For example I have one website based on the e-commerce platform. I want to send the desktop notification to my customer what are the new offers are available. I created the notification with following code.. But I want to call the notification without any user activity.
$(window).load(function(){
if(!window.Notification){
console.log('sorry Notification not supported');
}
else {
Notification.requestPermission(function(p){
if(p === 'denied') {
} else if(p === 'granted') {
} else {
}
console.log(p);
});
}
var notify;
if(Notification.permission === 'default') {
console.log('Please allow Notification');
} else {
notify = new Notification('Cashback Notification..',{
body : 'Get assured cashback on purchases made via Getex.com',
icon : 'images/notification_getex.png'
});
}
});
Here i trigger notification only for the window load, but i want to notify from the server side like execute or call any API link.
Please help me Thanks!

Sending browser notification to specific users (web)

I would want to send out notifications to the list of users from database.
I found a solution to send push notifications:
// request permission on page load
document.addEventListener('DOMContentLoaded', function () {
if (!Notification) {
alert('Desktop notifications not available in your browser. Try Chromium.');
return;
}
if (Notification.permission !== "granted")
Notification.requestPermission();
});
function notifyMe() {
if (Notification.permission !== "granted")
Notification.requestPermission();
else {
var notification = new Notification('Notification title', {
icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
body: "Hey there! You've been notified!",
});
notification.onclick = function () {
window.open("https://stackoverflow.com/a/13328397/1269037");
};
}
}
(Source: Chrome desktop notification example ) How can i configure it to send specific notifications to specific users ?
Let us assume that I want to send notifications to all users of the table user_notification.
I tried using
<?php
$sqx = "SELECT * FROM `user_js` ";
?>
in the code but it does not help either
step 1 : try adding the following into your HTML response :
<button onclick="notifyMe()">Notify me!</button>
step 2 : if you can see the button and you get a notification when you click on it, try to move on and trigger it as soon as the page loads.
see if this link will help you : http://www.w3schools.com/jsref/event_onload.asp
principally, this should work :
<body onload="notifyMe()">
step 3 : if all works well, you'll have to add a PHP condition that will add this "onload=..." only to the users you want.

WebRTC video call and IONIC, call when app runs in background

I made an application using twilio, webRTC and ionic, and the video call is working. However, is it possible to make the video call when the app runs in background? I couldn't come up with a solution with ionic.
I created a landing page upon login which has all the token grant and initialization of twilio so that once a user logs in. It is already on listening mode. Then, i handled the opening of push to actually just redirect to that particular landing page. In my case, the landing page is app.onlineUsers
var notificationOpenedCallback = function(jsonData) {
console.log('notificationOpenedCallback: ' + JSON.stringify(jsonData.action));
if (jsonData.action) {
if (jsonData.action.actionID == "IdAccept") {
$state.go('app.onlineUsers');
//custom code executes
} else if (jsonData.action.actionID == "IdReject") {
alert('rejected');
}
}
};
window.plugins.OneSignal.getIds(function(ids) {
// alert(ids);
did = ids.userId;
$sessionStorage.DID = did;
$localStorage.DID = did;
// alert(" player_id: "+$localStorage.DID);
});
// TODO: Update with your OneSignal AppId and googleProjectNumber before running.
window.plugins.OneSignal.startInit("appId", "googleProjectNumber")
.handleNotificationOpened(notificationOpenedCallback).handleNotificationReceived(function(jsonData) {
$state.go('app.onlineUsers');
// alert("Notification received:\n" + JSON.stringify(jsonData));
// console.log('Did I receive a notification: ' + JSON.stringify(jsonData));
}).inFocusDisplaying(window.plugins.OneSignal.OSInFocusDisplayOption.None).endInit();

How can i add simple link in html5 desktop notification

How can i add simple link (a href) in html5 desktop notification on body section? I try onclick function, but it work's only few seconds. If i try press later the notification just disappear and nothing do. So the best way would be with link. I try that write, but then just print me as text.
var notification = new Notification('title', {
icon: '...',
body: 'aaa'
});
Unfortunately there is no support for links and other markup in HTML notifications. The only method to get a clickable link with a notification is to use onclick:
function makeNotification() {
var notification = new Notification('This is a clickable notification', {body: 'Click Me'});
notification.onclick = function () {
window.open("http://stackoverflow.com/");
};
}
function notifyMe() {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
// Let's check if the user is okay to get some notification
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
makeNotification();
}
// Otherwise, we need to ask the user for permission
// Note, Chrome does not implement the permission static property
// So we have to check for NOT 'denied' instead of 'default'
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
// If the user is okay, let's create a notification
if (permission === "granted") {
makeNotification();
}
});
}
}
Mozilla has further documentation at https://developer.mozilla.org/en-US/docs/Web/API/notification
Firefox has a short duration for notifications compared to Chrome. There is no way to control how long a notification is visible in Firefox.
add dir : "ltr" in Options
options = {
dir : "ltr",
icon: "images/images.jpg",
body : "hello WOrld"
}
new Notification("Current Notify",options);

Categories