需要批量发送通知1000 [英] Need to send notification in a batch of 1000

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

问题描述

我已经创建了通知应用程序,现在我想向1000个用户的批次发送通知,但是由于我是新用户,所以我不知道如何创建该批次.

I have created notification application , now i want to send notification to batch of 1000 users, but as i am new i dont know how to create that batch.

这是我推送通知的代码.

this is my code to push notification.

<?php
include('header.php');

define( 'API_ACCESS_KEY', '[API-KEY comes here]' );

$sql="SELECT * FROM user_data";
$result = mysqli_query($conn, $sql) or die ('Error'.mysqli_error($conn));
$registrationIds=array();
while($row=mysqli_fetch_assoc($result)){

    $registrationIds[] = $row['allow'];
}
$ids=json_encode($registrationIds);
$fields = array
(
    'registration_ids'  => $registrationIds
);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );

echo $result;


?>

现在我可以向用户发送通知了,但是如果我有10000个用户,如何以1000个为单位发送通知?

Now i can push notification on to users but how can i send notification in batch of 1000 if i have 10000 users?

推荐答案

这样的事情(假设您希望在每个curl请求中发送1000个注册ID的批次):

Something like this (assuming that you want to send batch of 1000 registration ids with every curl request):

$batch_size = 1000; // 1000 per request

$iterations = ceil(count($registrationIds) / $batch_size); // determine how many iterations are needed

for ($i = 0; $i < $iterations; $i++) {
    $offset = $i * $batch_size;
    $batch_of_1000_user_ids = array_slice($registrationIds, $offset, $batch_size);

    // json_encode, prepare headers and send curl request
}

这篇关于需要批量发送通知1000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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