我想为我的网站创建一个通知发件人,我使用Notification Object和socket.io。我创建一个Page来写标题和通知正文。 通知正在工作,并将通知发送给用户在网站上在线。 但我也想发送给离线用户。
我使该站点在后端使用vuejs和nodejs。
这是我向服务器发送通知的套接字:
export default{
data(){
return{
title:"",
text:"",
socket: io("http://localhost:2000"),
}
};
methods:{
sendNotification(){
this.socket.emit("sendingNotification",{title:this.title,text:this.text});
}
};
}
我得到title
和text
的这种形式:
```
<div class="content-wrapper" style="min-height: 915.8px;">
<section class="content">
<div class="row">
<div class="col-md-6">
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">ارسال نوتیفیکیشن </h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form class="form-horizontal">
<div class="box-body">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">سرتیتر</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail3" placeholder="سرتیتر" v-model="title">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">متن</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputPassword3" placeholder="متن " v-model="text">
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" @click.prevent="sendNotification" class="btn btn-info pull-right">ارسال</button>
</div>
<!-- /.box-footer -->
</form>
</div>
</div>
</div>
</section>
</div>
```
这是我在Node.js中的socket.io代码:
let express = require('express');
let app=express();
let http = require('http').createServer(app);
let io = require('socket.io')(http);
io.on('connection', socket => {
socket.on("sendingNotification",(bodyMessage)=>{
console.log(bodyMessage);
io.emit("sendNotification",bodyMessage);
});
});
http.listen(2000);
我在vuejs文件中获取上述事件并发送通知:
socket.on("sendNotification", body => {
const not=new Notification(body.title,{
body:body.text,
});
});
任何人都可以帮助显示离线状态下发送给用户的通知