传输到 HTTPS 后推送通知不起作用

Posted

技术标签:

【中文标题】传输到 HTTPS 后推送通知不起作用【英文标题】:Push notification not working after transferring to HTTPS 【发布时间】:2018-03-13 09:08:19 【问题描述】:

我正在研究如何在我新安全的 tomcat 服务器上运行我的推送通知。我的通知在 nodejs 和 socket.io 模块上运行。在传输到 HTTPS 之前它工作正常。现在它在控制台上抛出错误:

Mixed Content: The page at 'https://example.com/home.jsp#' was loaded over HTTPS, but requested an insecure script 'http://example.com:8002/socket.io/socket.io.js'. This request has been blocked; the content must be served over HTTPS.

Uncaught ReferenceError: io is not defined
at home.jsp:812

这是我的节点 js 脚本 w/c 声明变量的部分:

var sys = require('sys');
var exec = require('child_process').exec;
function puts(error, stdout, stderr) 
sys.puts(stdout);
sys.puts(error);

var nodemailer = require("nodemailer");
var smtpTransport = require("nodemailer-smtp-transport");
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
fs = require('fs'),
mysql = require('mysql'),
connectionsArray = [],
connection = mysql.createConnection(
host: '127.0.0.1',
user: 'sampleUser',
password: 'samplePassword',
database: 'sampleDB',
port: 3306
),
POLLING_INTERVAL = 2000,
pollingTimer;  
var smtpTransport = nodemailer.createTransport(smtpTransport(
host : "smtp.gmail.com",
secureConnection : false,
port: 587,
auth : 
    user : "sample@email.com",
    pass : "sampleEmailPass"

));

这是我的jsp文件:

<html>
<head>
<!-- 
 * Author:      Gianluca Guarini
 * Contact:     gianluca.guarini@gmail.com
 * Website:     http://www.gianlucaguarini.com/
 * Twitter:     @gianlucaguarini
-->
    <title>Push notification server streaming on a MySQL db</title>
    <style>
    </style>
</head>
<body>
    <time></time>
    <div id="container"></div>
<script src="http://example.com:8002/socket.io/socket.io.js"></script>
<script>

    // create a new websocket
    var socket = io.connect('http://example.com:8002');
    // on message received we print all the data inside the #container div
    socket.on('notification', function (data) 
    var usersList = "";

    $.each(data.users,function(index,user)
        if (user.num_count!=0) 
            if(<%=session.getAttribute("accessID")%>==user.user_login_id)
        

        usersList += user.num_count;
        
    
    );

    if ($('#container').text() < usersList)
    
      var diff = usersList-$('#container').text();
      notifyMe(usersList);
      /*
      for(var e =0; e<diff; e++)
      

      notifyMe(diff);
      */
    

   $('#container').html(usersList);

   // $('time').html('Last Update:' + data.time);
  );
  function notifyMe(usersList) 
  // Let's check if the browser supports notifications
   var options = 
    icon: '/icon/main_page/main_icon.png',
    sound: '/icon/sounds/Water_DROP.mp3'
  
  if (!("Notification" in window)) 
  alert("This browser does not support desktop notification");
return false;
  

  // Let's check whether notification permissions have already been granted
  else if (Notification.permission === "granted") 
  // If it's okay let's create a notification
   var notification = new Notification('You have '+usersList+' item(s) for approval',options);

  notification.sound;
   

  // Otherwise, we need to ask the user for permission
  else if (Notification.permission !== 'denied') 
  Notification.requestPermission(function (permission) 
  // If the user accepts, let's create a notification
  if (permission === "granted") 
    var notification = new Notification('You have '+usersList+' item(s) for approval',options);

  notification.sound;
  
  );
  
  notification.onclick = function()
  window.focus();
  ;
   // At last, if the user has denied notifications, and you 
    // want to be respectful there is no need to bother them any more.
       
</script>
</body>

到目前为止,我所做的是将所有 http 转换为 https。它解决了混合类型错误,但没有解决未定义的 io 情况。我搜索了一些主题,但它使用 express.js,并且需要密钥和证书。

TIA

【问题讨论】:

您在多个地方向我们展示的代码中仍然显示http://。在进行这些更改后,您是否检查过这些资源是否仍然正确加载?您是否验证所有这些都支持 HTTPS? 是的,我尝试将所有 http 标签转换为 https。但它只解决了混合类型错误。 这个“是”应该是我刚才问你的所有问题的答案?就像您(?)在example.com:8002 设置的任何东西一样,现在应该只是自动处理 HTTPS 而不是 HTTP,尽管我们知道 HTTP 和 HTTPS 通常使用不同的端口开始? 哦,对不起,我忘了添加节点 js 文件,我已将我的应用程序设置为侦听端口 8002。我在 js 文件中有这样一行:app.listen(8002);跨度> 你也修改了var app = require('http').createServer(handler) 【参考方案1】:

非常简单的解决方案。 只需将您的“socket.io.js”脚本更改为通过 https 而不是 http 协议提供服务。 例如: 'https://yoursite.com/socket.io.js' 而不是 'http://yoursite.com/socket.io.js'

【讨论】:

第一条错误信息消失了?还是仍然发生?

以上是关于传输到 HTTPS 后推送通知不起作用的主要内容,如果未能解决你的问题,请参考以下文章

生成新证书后推送通知不起作用

phonegap 推送通知在 iPhone 上不起作用

门户更改后推送通知不起作用

世博会推送通知在构建后不起作用

Firebase 推送通知在上传到应用商店 ios 后不起作用

当我将应用程序转移到其他帐户时,推送通知不起作用