Redirection in progressive web app

别说谁变了你拦得住时间么 提交于 2019-12-06 04:05:44

问题


I am trying to redirect to a particular url in progressive web app on notification click but it does not redirect.

Case 1: If the web app is not added to home screen then on notification click the browser window opens up and is redirected to the desired url.

Case 2: If the web app is added to home screen then the landing page is the home page and not the desired url.

self.addEventListener('notificationclick', function(event) {
        event.notification.close();
        event.waitUntil(
                clients.matchAll({
                        type: "window"
                }).then(function(clientList) {
                        console.log(clientList);
                        for (var i = 0; i < clientList.length; i++) {
                                var client = clientList[i];
                                if (client.url == '/' && 'focus' in client)
                                        return client.focus();
                        }
                        if (clients.openWindow) {
                                if (event.notification.tag == 'syncTest') {
                                        console.log(event);
                                        var url = '/#/view-car?start_time=' + getFinalData[0].start_time + '&end_time=' + getFinalData[0].end_time + '&pickup_venue=' + getFinalData[0].pickup_venue;
                                        return clients.openWindow(url);
                                } else {
                                        var url = '/#/list-venues?start_time=' + getFinalData[0].start_time + '&end_time=' + getFinalData[0].end_time + '&car=' + getFinalData[0].car + '&fuel=' + getFinalData[0].fuel;
                                        console.log(url);
                                        return clients.openWindow(url);
                                }
                         }
                })
        );
});

Thanks a tonnn in advance!!.


回答1:


You can use something like:

if (client.url == 'yourUrl' && 'focus' in client)
     return client.focus();
else{
     client.navigate('yourUrl').then(function(c){
           return c.focus();
    });     
}

This worked for me. There can be other ways too.



来源:https://stackoverflow.com/questions/37985784/redirection-in-progressive-web-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!