【问题标题】:Is it possible to link progressive web app to toast是否可以将渐进式网络应用程序链接到吐司
【发布时间】:2019-10-30 16:13:15
【问题描述】:

我有下面这样的祝酒代码。

html

<button onclick="launch_toast()">Show Toast</button>

<div id="toast"><div id="img">Icon</div><div id="desc">A notification message..</div></div>

css

#toast {
    visibility: hidden;
    max-width: 50px;
    height: 50px;
    /*margin-left: -125px;*/
    margin: auto;
    background-color: #333;
    color: #fff;
    text-align: center;
    border-radius: 2px;

    position: fixed;
    z-index: 1;
    left: 0;right:0;
    bottom: 30px;
    font-size: 17px;
    white-space: nowrap;
}
#toast #img{
    width: 50px;
    height: 50px;

    float: left;

    padding-top: 16px;
    padding-bottom: 16px;

    box-sizing: border-box;


    background-color: #111;
    color: #fff;
}
#toast #desc{


    color: #fff;

    padding: 16px;

    overflow: hidden;
    white-space: nowrap;
}

#toast.show {
    visibility: visible;
    -webkit-animation: fadein 0.5s, expand 0.5s 0.5s,stay 3s 1s, shrink 0.5s 2s, fadeout 0.5s 2.5s;
    animation: fadein 0.5s, expand 0.5s 0.5s,stay 3s 1s, shrink 0.5s 4s, fadeout 0.5s 4.5s;
}

@-webkit-keyframes fadein {
    from {bottom: 0; opacity: 0;} 
    to {bottom: 30px; opacity: 1;}
}

@keyframes fadein {
    from {bottom: 0; opacity: 0;}
    to {bottom: 30px; opacity: 1;}
}

@-webkit-keyframes expand {
    from {min-width: 50px} 
    to {min-width: 350px}
}

@keyframes expand {
    from {min-width: 50px}
    to {min-width: 350px}
}
@-webkit-keyframes stay {
    from {min-width: 350px} 
    to {min-width: 350px}
}

@keyframes stay {
    from {min-width: 350px}
    to {min-width: 350px}
}
@-webkit-keyframes shrink {
    from {min-width: 350px;} 
    to {min-width: 50px;}
}

@keyframes shrink {
    from {min-width: 350px;} 
    to {min-width: 50px;}
}

@-webkit-keyframes fadeout {
    from {bottom: 30px; opacity: 1;} 
    to {bottom: 60px; opacity: 0;}
}

@keyframes fadeout {
    from {bottom: 30px; opacity: 1;}
    to {bottom: 60px; opacity: 0;}
}

js

function launch_toast() {    
  var x = document.getElementById("toast");   
  x.className = "show";    
  setTimeout(function(){ 
    x.className = x.className.replace("show", ""); 
  }, 5000); 
}

我想知道如何将此代码链接到我的渐进式网络应用程序,以便在单击时用户能够下载 PWA。我的site

【问题讨论】:

  • 你检查过这个blog吗?它使用弹出的原生 toast 并在 PWA 的 Action Center 中显示通知。

标签: javascript html progressive-web-apps toast


【解决方案1】:

您应该获得对beforeinstallpromt 事件的引用,您可以稍后在toast 中调用promt 函数。可以通过添加事件监听器获取该事件,如下(来源:Google):

let installPromptEvent;

window.addEventListener('beforeinstallprompt', (event) => {
  // Prevent Chrome <= 67 from automatically showing the prompt
  event.preventDefault();
  // Stash the event so it can be triggered later.
  installPromptEvent = event;
  // Update the install UI to notify the user app can be installed
  document.querySelector('#install-button').disabled = false;
});

因此,在您的 launch_toast 函数中,您可以启动提示:

function launch_toast(){
  installPromptEvent.prompt();
}

当然,这是假设您的清单配置良好并且您的服务工作者也可以注册。如果设备不支持这一点,beforeinstallprompt 将不会被触发,您对installPromptEvent 的引用将是undefined。当 PWA 已安装在设备上时,该事件也不会触发。

【讨论】:

  • 我认为在你的 main.js 文件中包含这个是有意义的
  • 您的清单和服务人员配置是否正确?然后我的答案中的代码应该可以工作
  • 为什么要禁用该按钮?这似乎是一个单独的问题。如果您只是保留与问题中相同的按钮,则代码应该可以工作。
  • @JustineChacko 浏览器支持可以找到here
  • 我不知道。希望支持将很快得到改善。
猜你喜欢
  • 2023-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-19
  • 1970-01-01
相关资源
最近更新 更多