发送多个iPhone推送通知+ APNS + PHP +教程 [英] Sending multiple iphone push notifications + APNS + PHP + Tutorial

查看:175
本文介绍了发送多个iPhone推送通知+ APNS + PHP +教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个PHP网站+ iPhone应用程序和API的iPhone应用程序,对学生和医生信息系统,当任何一个发送邮件(从网站或iPhone),其他用户应该对他的iPhone推送通知。例如,如果学生增加了一个新的问题教师,对教师的iPhone推送通知/ iPad将被发送到老师andwhen老师回答学生的回答,学生将获得一个推送通知。由于没有对教师和学生的注册网站的数量没有限制。所以我的问题是如何把消息发送到注册用户的iPhone,我想尽快发送推送消息作为有人回复或添加问题。发送多个推送消息请给我提供的PHP code。
我对报名时每个用户节省了设备令牌。

I am working on a php website + iphone application and API for iphone application, has a messaging system for students and doctors, when any one sends message(from website or iphone) the other user should get push notification on his iphone. For example if student adds a new question for teacher, a push notification on teachers iphone/ipad will be send to teacher andwhen teacher reply to student's answer, student will get a push notification. Since there is no restriction on number of teachers and student registering to website. So my question is how to send push messages to registered users iphone, I want to send push message as soon as some one reply or add question. Please provide me php code for sending multiple push messages. I am saving device token for each user while registration.

当老师一个问题的答复,我将邮件发送到学生,我想太多发送推送通知学生,反之亦然,所以请注明code能够管理错误情况。

When teacher reply to question I am sending mail to student, I want to send a push notification too to student and vice versa so please specify code able to manage error conditions.

请提出任何教程在iOS发送推送通知。

Please suggest any tutorial for sending push notifications on iOS.

推荐答案

这是我终于完成了它的方式

This is the way I have done it finally


  1. 的PHP

  2. PHP code

    set_time_limit(0);
    $root_path = "add your root path here"; 
    require_once($root_path."webroot\cron\library\config.php");
    require_once($root_path."Vendor\ApnsPHP\Autoload.php");

        global $obj_basic;           
        // Basic settings

        $timezone = new DateTimeZone('America/New_York');
        $date = new DateTime();
        $date->setTimezone($timezone);
        $time =  $date->format('H:i:s');


        //Get notifications data to send push notifications
        $queueQuery = " SELECT `notifications`.*, `messages`.`mes_message`, `messages`.`user_id`, `messages`.`mes_originated_from`  FROM `notifications`
                                        INNER JOIN `messages`
                                        ON `notifications`.`message_id` = `messages`.`mes_id`

                                        WHERE `notifications`.`created` <= NOW()";

        $queueData = $obj_basic->get_query_data($queueQuery);

        if(!empty($queueData)) {

        // Put your private key's passphrase here:
        $passphrase = 'Push';

        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', 'server_certificates_bundle_sandbox.pem');
        stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

        // Open a connection to the APNS server
        $fp = stream_socket_client(
            'ssl://gateway.sandbox.push.apple.com:2195', $err,
            $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

        if (!$fp)
        exit("Failed to connect: $err $errstr" . PHP_EOL);

        echo '<br>'.date("Y-m-d H:i:s").' Connected to APNS' . PHP_EOL;

            foreach($queueData as $val) {
                    // Put your device token here (without spaces):
                    $deviceToken = $val['device_token'];

                    // Create message

                        // Get senders name
                        $sql = "SELECT `name` FROM `users` WHERE id =".$val['user_id'];
                        $name = $obj_basic->get_query_data($sql);
                        $name = $name[0]['name']; 
                        $message = $name." : ";

                        // Get total unread messaged for receiver
                        $query = "SELECT COUNT(*)  as count FROM `messages`  WHERE mes_parent = 0 AND user_id = ".$val['user_id']." AND mes_readstatus_doc != 0 AND mes_status = 1";
                        $totalUnread = $obj_basic->get_query_data($query);
                        $totalUnread = $totalUnread[0]['count']; 



                        $message .= " This is a test message.";


                    // Create the payload body
                    $body['aps'] = array(
                            'alert'         => $message,
                            'badge'     => $totalUnread,
                            'sound'     => 'default'
                     );

                    // Encode the payload as JSON
                    $payload = json_encode($body);

                    // Build the binary notification
                    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

                    // Send it to the server
                    $result = fwrite($fp, $msg, strlen($msg));

                    if (!$result) {
                        echo '<br>'.date("Y-m-d H:i:s").' Message not delivered' . PHP_EOL;  
                    } else {
                        $sqlDelete = "DELETE FROM `notifications` WHERE id = ".$val['id'];
                        $query_delete = $obj_basic->run_query($sqlDelete,'DELETE');

                        echo '<br>'.date("Y-m-d H:i:s").' Message successfully delivered' . PHP_EOL;
                    }
            }
            // Close the connection to the server
            fclose($fp);
            echo '<br>'.date("Y-m-d H:i:s").' Connection closed to APNS' . PHP_EOL;
        } else {
            echo '<br>'.date("Y-m-d H:i:s").' Queue is empty!';
        }

这篇关于发送多个iPhone推送通知+ APNS + PHP +教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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