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

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

问题描述

这不是我第一个发送推送通知的应用,但它是我第一个同时向所有用户发送通知的应用.

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.

我遇到的是,并非我的所有用户都收到通知,只有其中一些用户收到通知,即使他们的设置正确(即启用了我的应用程序的通知),代码也是正确的,因为它已发送给其中一些,所以我的猜测是代码错过"了一些执行,因为与 APNS 的连接是异步的,所以不知何故(如果我错了,你告诉我)它会扰乱发送通知的队列.

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.

代码如下:

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.

我为此使用了 Slim.

Im using Slim for this.

希望你能帮助我,谢谢!

hope you can help me, thanks!

推荐答案

您发送到的某些设备令牌可能无效.即使是一台无效的设备也可以解释您所遇到的情况.当您发送带有无效设备令牌的通知时,Apple 会返回错误响应并关闭套接字.

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.

当您的代码检测到套接字已关闭时,您可能已经在无效通知之后发送了许多通知(甚至可能您在检测到套接字关闭之前已发送完所有通知),这会导致所有通知无效的发送后丢弃.创建新套接字后,此类通知必须重新发送给 Apple.

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.

我建议您阅读 本文档 了解更多详情.

I suggest you read the Push Notification Throughput and Error Checking section in this document for further details.

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

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天全站免登陆