如何向多个设备发送推送通知? [英] How to send Push Notification to multiple devices?

查看:38
本文介绍了如何向多个设备发送推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次在我的应用程序中使用推送通知.我已经浏览了示例应用程序和书籍,并且了解了如何将推送通知发送到单个设备.但是我没有确切地知道我应该在我的程序中做哪些更改来向多个设备发送推送通知.我正在使用PushMeBaby"应用程序进行服务器端编码.请帮帮我.

This is the first time I am using push notification in my App. I have gone through sample applications along with books and I got how to send push notification to a single device. But I am not getting exactly what changes should I do in my program to send push notification to multiple devices. I am using 'PushMeBaby' application for server side coding. Please, help me out.

推荐答案

尝试此示例代码并针对您的环境进行修改.

Try this example code and modify for your environment.

    $apnsHost = '<APNS host>';
    $apnsPort = <port num>;
    $apnsCert = '<cert>';

    $streamContext = stream_context_create();
    stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

    $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);

    $payload['aps'] = array('alert' => 'some notification', 'badge' => 0, 'sound' => 'none');
    $payload = json_encode($payload);

// Note: $device_tokens_array has list of 5 devices' tokens

    for($i=0; $i<5; $i++)
    {
            $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device_tokens_array[i])) . chr(0) . chr(strlen($payload)) . $payload;

            fwrite($apns, $apnsMessage);
    }?>

本文有助于验证断开连接和连接状态:Apple 推送通知: 发送大量消息

This article helps verifying drop connection and connection status: Apple Push Notification: Sending high volumes of messages

其他参考链接:

如何我可以在 iPhone 中一次性向多台设备发送推送通知吗?使用推送通知时如何处理多个设备?

这篇关于如何向多个设备发送推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆