如何正确地发送推送通知 [英] how to properly send push notifications

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

问题描述

这是不是我的第一个应用程序发送推送通知,但它是我的第一个应用程序,我在同一时间发送给我的所有用户发送通知。

This is not my first app sending push notifications but it is my first app where I send a notification to all my users at the same time.

林经历是什么,不是所有的我的用户收到通知,只有其中的一部分,即使他们有(启用了我的应用程序,即通知)正确的设置,还因为它被发送到code是正确的他们中的一些,所以我的猜测是,code错过的一些处决,因为与APNS的连接如为asyncronous这样莫名其妙地(你告诉我,如果我错了),它弄乱发送通知的队列中。

What Im experiencing is that not all my users receive the notification, only some of them, even though they have the settings correct(ie notifications for my app enabled), also the code is correct since it is sent to some of them, so my guess is that the code "misses" some executions, since the conection with the APNS is asyncronous so somehow( you tell me if im wrong) that it messes the queue of sending notifications.

这里的code:

function sendNotification(){

$sql = "SELECT * FROM users WHERE phone = 'iPhone' AND pushID != ''";
try {
    $db = getConnection();
    $stmt = $db->prepare($sql);  
    $stmt->execute();
    $users = $stmt->fetchAll(PDO::FETCH_OBJ);

    $request = Slim::getInstance()->request();
    $content = json_decode($request->getBody());
    $message = $content->message;

    foreach($users as $user){

            // Put your device token here (without spaces):
            $deviceToken = $user->pushID;


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

            ////////////////////////////////////////////////////////////////////////////////

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

            // Open a connection to the APNS server
            $fp = stream_socket_client(
                'ssl://gateway.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 'Connected to APNS' . PHP_EOL;

            // Create the payload body
            $body['aps'] = array(
                'alert' => $message,
                '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 'Message not delivered' . PHP_EOL;
            else
                echo 'Message successfully delivered' . PHP_EOL;

            // Close the connection to the server
            fclose($fp);

    }
    $db = null;
} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}
}

正如你可以看到我去找我的所有用户使用iPhone,我给他们的通知。我的手机是在该列表中的最后一个用户,我没有得到它,我知道是首当其冲的一个其他用户,她得到了它。
它要么错过了一些用户的阵列中,或者只是做上半年,没有显示任何错误。

As you can see I fetch all my users with an iPhone and I send them the notification. My phone is the last user in that list and I didnt get it, another user that I know is one of the first ones and she got it. It either misses some of the users in the array or it just does the first half, no error is shown.

林。

希望你能帮助我,谢谢!

hope you can help me, thanks!

推荐答案

这可能是一些要发送到设备令牌是无效的。即使是一个无效的设备可以解释你正在经历。当你发送通知与无效的设备令牌,苹果将返回一个错误响应和关闭套接字。

It's possible that some of the device tokens you are sending to are invalid. Even one invalid device can explain what you are experiencing. When you send a notification with an invalid device token, Apple returns an error response and closes the socket.

这可能是由您的code检测到关闭套接字你已经无效相继派出许多的通知(它甚至有可能在你完成检测插座关闭之前将所有的通知),这会导致所有的时间无效一个接发送的通知的要被丢弃。您创建一个新的socket后,这样的通知,必须重新发送给苹果公司。

It's possible that by the time your code detects that the socket is closed you already sent many notifications after the invalid one (it's even possible you finished sending all the notifications before detecting the socket is closed), which causes all of the notifications sent after the invalid one to be discarded. Such notifications must be resent to Apple after you create a new socket.

我建议你阅读推送通知吞吐量和错误检查中的有关详细信息,此文档

请确保您的数据库不包含生产设备令牌和沙箱设备令牌的组合,因为生产令牌是在沙箱环境无效,反之亦然。

Make sure that your database doesn't contain a mix of production device tokens and sandbox device tokens, since production tokens are invalid in sandbox environment and vice versa.

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

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