How to Detect the device level Notification ON or OFF using javascript for PWA Application?
Asked Answered
A

1

15

My requirement is how to detect the device level notification on/off in android device using javascript (don't use any kind of plug-in only you can use plug-in if this plugin support to PWA application).

As per if notification off I need to show the pop up to user please enable the notification feature you'll get notifications.

Below answer is only for detective browser level notification. If anybody know Please give me exact answer how to do it. Because,I stopped there.

Please check the image here user enable once if user disable then I'm unable to send notification.

enter image description here

Agonize answered 9/11, 2019 at 5:36 Comment(6)
Browser is going to return that, I don't think so you need to take care of that. A browser can only ask permission if overall notifications are allowed. We could connect if required.Opportunist
@Opportunist your right. I handled browser level already. But if user off the device level I'm unable to push the notication. so, I can't be enable direct in user device that's why I'm thing to display at least pop up.Agonize
Browser only give browser level notification status..I guess you mean to say like (Notification.permission).Agonize
Can anyone give answer..please..!!Agonize
If you have enabled android interface with the webapp, then you could create a hook which can handle it for you. Or, you could try Notification.requestPermission. Not really sure how well it'll work on a custom mobile webapp.Alice
Okay Varun how to hook could you please give examples?.. I'll try.. thanksAgonize
B
5

The feature seems to be well documented, have you tried:

function notifyMe() {
  // Let's check if the browser supports notifications
  if (!("Notification" in window)) {
    console.log("This browser does not support desktop notification");
  }

  // Let's check whether notification permissions have alredy been granted
  else if (Notification.permission === "granted") {
    // If it's okay let's create a notification
    var notification = new Notification("Hi there!");
  }

  // Otherwise, we need to ask the user for permission
  else if (Notification.permission !== 'denied' || Notification.permission === "default") {
    Notification.requestPermission(function (permission) {
      // If the user accepts, let's create a notification
      if (permission === "granted") {
        var notification = new Notification("Hi there!");
      }
    });
  }

  // At last, if the user has denied notifications, and you 
  // want to be respectful there is no need to bother them any more.
}
Bollix answered 18/11, 2019 at 13:38 Comment(15)
Hey..Mose.. your are answer is for browser level notification.. I'm asking about device level..how to detect?Agonize
Yes, I tried and implemented for browser level. But I stock over the device level.. notification detect.Agonize
@Agonize according to MDN "The Notifications API lets a web page or app send notifications that are displayed outside the page at the system level; this lets web apps send information to a user even if the application is idle or in the background.."Abrade
yes but if user off the notification in device level I'm unable to push the notification.. please check my screen shot.Agonize
So, My requirement is I need to show one pop-up if device level notification off.Agonize
The best you can do is to check if methods above returns some specific error, browsers are sandboxed and have limited access to OS for security reason.Abrade
Hey, @Mose, it's even not return any errors in push event..I even try so many way but still I'm not getting my results..I guess you're right this is OS security..Agonize
Please suggest or try if there any solution.. Thanks..@MoseAgonize
@This is browser security, to access to higher level info you should wrap your webapp in a app and ask directly to the OS, then propagate the info to the web view.Abrade
@This is browser security, to access to higher level info you should wrap your webapp in a app and ask directly to the OS, then propagate the info to the web view.Abrade
Our application is PWA application means it should be run in browser only by default..If we make app also.Agonize
Could you please give examples how to wrap & ask to OS for permission.Agonize
To wrap the application you should use any framework capable of this, including Android SDK. But beware, your app should be downloaded to work, it won't be enough navigate to it with your browsers. The namespace to check should be NotificationManagerCompat.from(context).areNotificationsEnabled().Abrade
Hey, This is working in Native app..but I'm not sure in PWA app.. Thanks I'll try.Agonize
This answer is wrong, the question is how to detect system level blocking using javascript, not browser level.Sontag

© 2022 - 2024 — McMap. All rights reserved.