Javascript disable chrome notifications - javascript

I need a page with a button that when pressed disables or blocks all push notifications enabled on chrome.
I tried this :
let dnperm = document.getElementById('dnperm');
dnperm.addEventListener('click', function(e){
e.preventDefault();
if(!window.Notification){
alert("Notification not supported!");
}else{
Notification.requestPermission().then(function(permission) {
console.log(permission);
if(permission === 'granted'){
permission = 'denied';
alert("you succesd");
}else if(permission === 'denied'){ alert("you fail");
permission = 'granted';
}
})
}
});
HTML :
<body>
block

You can`t change Notification.permission via browser API, it can be done only via native browser propt

Related

How to get user location on browser using PHP [duplicate]

My application is powered by jQuery mobile and uses geolocation.
After my application attempts to get the user's location, the (Chrome) browser prompts the user:
Example.com wants to track your physical location [allow] [deny]
My goal is:
If the user clicks "Allow", function 1 is called (location is used
by app).
If the user clicks "Deny", function 2 is called (address form
appears).
How can I bind a function to the event that occurs (if any) when the user clicks the "Allow" or "Deny" button?
The getCurrentPosition function accepts two function arguments. Heck, the first is executed when you allow and the other when you deny!
Documentation
http://jsfiddle.net/pimvdb/QbRHg/
navigator.geolocation.getCurrentPosition(function(position) {
alert('allow');
}, function() {
alert('deny');
});
Link to Docs
function handlePermission() { navigator.permissions.query({name:'geolocation'}).then(function(result) {
if (result.state == 'granted') {
report(result.state);
geoBtn.style.display = 'none';
} else if (result.state == 'prompt') {
report(result.state);
geoBtn.style.display = 'none';
navigator.geolocation.getCurrentPosition(revealPosition,positionDenied,geoSettings);
} else if (result.state == 'denied') {
report(result.state);
geoBtn.style.display = 'inline';
}
result.onchange = function() {
report(result.state);}});}function report(state) {
console.log('Permission ' + state);}
I hope this works.

Request location coordinates after user has blocked access in javascript

How can I prompt a user for their geo-location in javascript if they've blocked my request in the past? (using navigator.geolocation.getCurrentPosition).
For example, my web app requires location services, and the user accidentally clicks "block", or they change their mind. What can I do to prompt them again?
As mentioned by #matthew-shwery, you can not change the permission.
the best you could do is check for the permission and notify the user is the permission is denied
navigator.permissions.query({
name: 'geolocation'
}).then(function(result) {
if (result.state == 'granted') {
report(result.state);
geoBtn.style.display = 'none';
} else if (result.state == 'prompt') {
report(result.state);
geoBtn.style.display = 'none';
navigator.geolocation.getCurrentPosition(revealPosition, positionDenied, geoSettings);
} else if (result.state == 'denied') {
report(result.state);
geoBtn.style.display = 'inline';
}
result.onchange = function() {
report(result.state);
}
});
Geolocation docs
You can't.
The user must manage their browser settings manually because your site is added to a blacklist when denied location permissions.
Here are instructions for Chrome users to manage their location permissions: https://support.google.com/chrome/answer/142065?hl=en

Javascript, Socket.io - alert shows up more than one time

When I click the follow button, socket.io sends some data to the server, and then the server sends back a response number. According to what the number is, js alerts a message. But if I click the button a second time, js will alert the same message twice, and if I click it again, three times and so on. If I refresh the page, it starts all over again (click it once, alert shows up once, click it twice, alert shows up twice...)
Here's the code:
$('.followUser').click(function(e){
e.stopImmediatePropagation();
e.preventDefault();
var user= $(this).parent().parent().parent().parent().next().children().children('.userName').children().first().children().attr('id');
var thisUserId = $.cookie('thisUserID');
if(user != thisUserId){ //if he tries to follow himself
var object = {
user: user,
userId: thisUserId
}
socket.emit('followUser', object); //server just adds that user to the following list of the first user
socket.on('followUserResults', function(data){
if(data == 1){
alert('Something went wrong! Please refresh this page and try again'); // if they changed the id on html
} else if(data == 0){
alert('User was added to your following list!');
} else if(data == 2){
alert('This user is already on your following list!');
}
});
} else {
return false;
}
Can you please help me with that? Thank you!
I am slightly unclear as to what is trying to be achieved but I've noticed an error in your code straight away.
This code should be outside of the $('.followuser').click function:
socket.on('followUserResults', function(data){
if(data == 1){
alert('Something went wrong! Please refresh this page and try again'); // if they changed the id on html
} else if(data == 0){
alert('User was added to your following list!');
} else if(data == 2){
alert('This user is already on your following list!');
}
});
So your code should read like:
$('.followUser').click(function(e){
e.stopImmediatePropagation();
e.preventDefault();
var user= $(this).parent().parent().parent().parent().next().children().children('.userName').children().first().children().attr('id');
var thisUserId = $.cookie('thisUserID');
if(user != thisUserId){ //if he tries to follow himself
var object = {
user: user,
userId: thisUserId
}
socket.emit('followUser', object); //server just adds that user to the following list of the first user
} else {
return false;
}
socket.on('followUserResults', function(data){
if(data == 1){
alert('Something went wrong! Please refresh this page and try again'); // if they changed the id on html
} else if(data == 0){
alert('User was added to your following list!');
} else if(data == 2){
alert('This user is already on your following list!');
}
});
Try put the socket.on(...) outside the click callback function, if still not working properly, I would need watch the server code.

Remove HTML5 notification permissions

You can prompt a user to allow or deny desktop notifications from the browser by running:
Notification.requestPermission(callback);
But is it possible to remove that permission by code? We want our users to have the option to toggle notifications. Can this be achieved by JavaScript or do we need to save that option elsewhere?
Looking at the documentation on Notification at MDN and WHATWG, there does not seem to be a way to request revocation of permission. However, you could emulate your own version of the permission using localStorage to support that missing functionality. Say you have a checkbox that toggles notifications.
<input type="checkbox" onChange="toggleNotificationPermission(this);" />
You can store your remembered permissions under the notification-permission key in local storage, and update the permission state similar to:
function toggleNotificationPermission(input) {
if (Notification.permission === 'granted') {
localStorage.setItem('notification-permission', input.checked ? 'granted' : 'denied');
} else if (Notification.permission === 'denied') {
localStorage.setItem('notification-permission', 'denied');
input.checked = false;
} else if (Notification.permission === 'default') {
Notification.requestPermission(function(choice) {
if (choice === 'granted') {
localStorage.setItem('notification-permission', input.checked ? 'granted' : 'denied');
} else {
localStorage.setItem('notification-permission', 'denied');
input.checked = false;
}
});
}
}
You could retrieve the permission as:
function getNotificationPermission() {
if (Notification.permission === 'granted') {
return localStorage.getItem('notification-permission');
} else {
return Notification.permission;
}
}
When you want to display a notification, check your permission:
if (getNotificationPermission() === 'granted') {
new Notification(/*...*/);
}
No, there is no way for your script to programmatically relinquish permission to show notifications. The API specification does not have any permission-related functions aside from requestPermission. (Of course, a browser may have an options menu that allows the user to revoke permission for a domain, but that's a browser-level option, not a site-level option. For example, in Chrome, you can see this options menu by clicking the icon in the left of the address bar.)
If you don't want to show notifications, simply don't call new Notification.
You can either wrap all your calls to new Notification inside conditions:
if(notifications_allowed) {
new Notification(...);
}
Or you can rewrite the Notification constructor to contain a contiditional and call the original Notification as appropriate:
(function() {
var oldNofitication = Notification;
Notification = function() {
if(notifications_allowed) {
oldNotification.apply(this, arguments);
}
}
})();
If you use vendor-prefixed constructors or functions (e.g., webkitNotifications.createNotification), then you'll need to rewrite each of those as well to be conditional on your options variable.

Issue with HMTL5 Desktop Notifications in Google Chrome

I'm having problems adding basic desktop notifications to my wicket site. Based on the example code at https://developer.mozilla.org/en-US/docs/Web/API/Notification I've overridden the renderHead(IHeaderResponse) method in my page so that the following is added to the page:
<script type="text/javascript" id="desktopNotification-init">
/*<![CDATA[*/
function notifyMe(){
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}else if (Notification.permission === "granted") {
var notification = new Notification("You have a system notification");
}else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
if(!('permission' in Notification)) {
Notification.permission = permission;
}
if (permission === "granted") {
var notification = new Notification("You have a system notification");
}
});
}
}
/*]]>*/
</script>
I then have a button that adds a call to the function when clicked:
public void onClick( AjaxRequestTarget p_target, WebMarkupContainer p_menuItemComponent ) {
if( p_target != null ) {
p_target.appendJavaScript( "notifyMe();" );
}
}
This works in Firefox but clicking the button has no effect in Chrome. It seems to be an issue with the permission request as it works correctly if I change settings to always allow notifications. Is there an issue with the javascript that I'm missing?

Categories

Resources