Wednesday, June 5, 2024
186
rated 0 times [  190] [ 4]  / answers: 1 / hits: 51816  / 6 Years ago, mon, may 14, 2018, 12:00:00

I understand that with a properly made Progressive Web App mobile browsers will display a banner prompting users to 'Install' the app on their home screen.


I have been looking around for a way to trigger that prompt from within the app, but have not been able to find anything.


Is there a line of JavaScript that can be used to call the install prompt banner at any time?? Something that I could add to a install button tucked away in a help screen for instance?


It might be difficult for some users to find the "Add to Home Screen" option if they missed the install banner prompt. I'd like to give them a button they can click to be prompted again.


2020 EDIT: Yes, this is possible in Chrome - see answer below


See this great article: How to provide your own in-app install experience and my working demo of the article's process applied in a React app.


Or for a slightly different approach, see how snapdrop.net did it.


More From » service-worker

 Answers
5

Thanks for all the great answers. Recently it appears that things have become more standardized, and there is a solid way of doing things in chrome at least that allows users to click and install the app even if they missed the prompt etc.


Pete LePage wrote a very clear article on web.dev called How to provide your own in-app install experience which details the process. The code snippets and process is taken directly from the article.



  1. Listen for the beforeinstallprompt event.


let deferredPrompt;

window.addEventListener('beforeinstallprompt', (e) => {
// Prevent the mini-infobar from appearing on mobile
e.preventDefault();
// Stash the event so it can be triggered later.
deferredPrompt = e;
// Update UI notify the user they can install the PWA
showInstallPromotion();
});


  1. Save the beforeinstallprompt event, so it can be used to trigger the install flow later.


buttonInstall.addEventListener('click', (e) => {
// Hide the app provided install promotion
hideMyInstallPromotion();
// Show the install prompt
deferredPrompt.prompt();
// Wait for the user to respond to the prompt
deferredPrompt.userChoice.then((choiceResult) => {
if (choiceResult.outcome === 'accepted') {
console.log('User accepted the install prompt');
} else {
console.log('User dismissed the install prompt');
}
});
});



  1. Alert the user that your PWA is installable, and provide a button or other element to start the in-app installation flow.


For more details and other capabilities, see the full article.


I made a working demo of a this process implemented in React. (source code)


Also, for a slightly different approach, you can see how the maker(s) of Snapdrop implemented an install button in the source code.


[#54432] Friday, May 11, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jack

Total Points: 557
Total Questions: 96
Total Answers: 80

Location: Saint Helena
Member since Mon, Jan 16, 2023
1 Year ago
jack questions
Sun, Jan 31, 21, 00:00, 3 Years ago
Thu, Dec 10, 20, 00:00, 4 Years ago
Sat, Aug 1, 20, 00:00, 4 Years ago
Fri, Apr 17, 20, 00:00, 4 Years ago
Wed, Aug 14, 19, 00:00, 5 Years ago
;