PubNub Facebook 通知
这是一个类似 Facebook 的例子
通过 PubNub 向您的用户通知自定义消息的窗口框。
您可以在他们的手机或浏览器上向您的用户发送更新。
这将向您的用户显示通知;任何通知你。
使用 PubNub 允许通过 WebSockets、BOSH、Comet 和其他机制进行数据推送
在您的应用程序中使用,为您提供发送数据的能力
随时通过MASS BROADCAST直接向您的用户发送或
个人通知。
从这里开始:现场演示
立即尝试:
http://pubnub-demo.s3.amazonaws.com/facebook-notification/index.html
下载源代码:
https://github.com/pubnub/javascript/tree/master/examples/facebook-notification
从这里开始可以轻松复制/粘贴代码。
这很容易上手,我们建议您开始
在开始之前使用上面的示例链接。
设置您的页面
首先包含 FBootstrap 资源,以便提供
通知窗口的外观。
将这些样式添加到您的 HTML 文件中。
<link href=bootstrap.css rel=stylesheet>
<style type=text/css> body { padding-top: 60px; } </style>
数据连接代码
接下来您需要设置 PubNub 数据连接,然后
添加收到数据后如何处理数据的规则。
<script src="https://pubnub.s3.amazonaws.com/pubnub-3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-modal/2.2.6/js/bootstrap-modal.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>(function(){
// PubNub (For Data Push to User)
var pubnub = PUBNUB.init({
subscribe_key : 'demo',
ssl : false
});
// Setup New Data Push Connectoin via PubNub
pubnub.subscribe({
restore : true,
channel : 'example-user-id-1234',
callback : show_notification
});
// Setup Alert Window
$('#new-alert').modal({ keyboard : true });
// Show the Notification Window
function show_notification(message) {
$('#new-alert').modal('show');
}
// Simulate Notification
$('#simulate-notification').bind( 'mousedown', function() {
pubnub.publish({
channel : 'example-user-id-1234',
message : 'alert'
});
return false;
} );
})();</script>
Python 推送示例
接下来您将要添加此python 代码
到您的 Django 或任何其他框架。
您可以将此添加到应用程序中的message post 代码中。
这将向您的用户发布通知。
此特定示例将导致出现通知
在 Facebook 通知页面内。
pip install pubnub
Python 源码
## PubNub Setup
import pubnub from Pubnub
pubnub = Pubnub( 'demo', 'demo', None, False )
## Push Notice to 'example-user-id-1234'
info = pubnub.publish({
'channel' : 'example-user-id-1234',
'message' : { 'your-data' : 'any-data-here' }
})
print(info)