私が行った実際の実装を共有したかっただけです。@Virendraの優れた回答のリンクを読んで気付いたように、プッシュ通知の実装には多くの困難な問題があるため、優れたSAASであるPusherを使用することにしました。
私が最も感銘を受けたのは、これを機能させるために記述しなければならないコードがいかに少ないかということです。下記参照。私のサーバー側はPHPです(プッシャーには多くの言語のライブラリがあります)。
require('/application/thirdParty/pusher-html5-realtime-push-notifications/lib/squeeks-Pusher-PHP/lib/Pusher.php');
require('/application/thirdParty/pusher-html5-realtime-push-notifications/config.php');
$pusher = new Pusher(APP_KEY, APP_SECRET, APP_ID);
foreach($recipients as $row){
$channel='my-channel'.$row->recipient_id;
$pusher->trigger($channel, 'notifications',
array('message' => $row->message,
'notification_id' => $row->notification_id)
);
}
HTML/JS は次のとおりです (圧倒されないでください。このコードのほとんどは、Stackoverflow や他の人が行っているように、着信通知を小さな円とリストに入力するだけです)。
<script src="/application/thirdParty/pusher.min.js"></script>
<script>
var myID=179; // would receive notification if myID matches $row->recipient_id above;
var myChannel = pusher.subscribe('my-channel'+myID);
myChannel.bind('notifications',
function(data) {
var message=String(data.message),
url='/notifications/'+data.notification_id,
icon='<i class=\'icon-heart\'></i>',
urlText=icon+message;
var notificationRow='<li><a href='+url+'>'+urlText+'</a></li>';
$('#notificationsDropdownList').prepend(notificationRow);
if(notificationCircleCount==0){
notificationCircleCount++;
$notificationCircle.show();
$notificationCircleCount.html(notificationCircleCount);
}
else{
notificationCircleCount++;
$notificationCircleCount.html(notificationCircleCount);
}
console.log('Pusher happened'+data.message);
} //function
); //myChannel
</script>