HTML5 Notification not working in Mobile Chrome

后端 未结 4 1646
谎友^
谎友^ 2020-12-02 08:30

I\'m using the HTML5 notification API to notify the user in Chrome or Firefox. On desktop browsers, it works. However in Chrome 42 for Android, the permission is requested b

4条回答
  •  一向
    一向 (楼主)
    2020-12-02 08:59

    I had no trouble with the Notification API on Windows Desktop. It even worked without issues on Mobile FF. I found documentation that seemed to indicate Chrome for Android was supported too, but it didn't work for me. I really wanted to prove the API could work for me on my current (2019) version of Chrome (70) for Android. After much investigation, I can easily see why many people have had mixed results. The answer above simply didn't work for me when I pasted it into a barebones page, but I discovered why. According to the Chrome debugger, the Notification API is only allowed in response to a user gesture. That means that you can't simply invoke the notification when the document loads. Rather, you have to invoke the code in response to user interactivity like a click.

    So, here is a barebones and complete solution proving that you can get notifications to work on current (2019) Chrome for Android (Note: I used jQuery simply for brevity):

    
    
    
    
    
    
    
    
    
    
    
    
    
    

    In summary, the important things to know about notifications on current (2019) Chrome for Android:

    1. Must be using HTTPS
    2. Must use Notification API in response to user interactivity
    3. Must use Notification API to request permission for notifications
    4. Must use ServiceWorker API to trigger the actual notification

提交回复
热议问题