首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用GCM向所有用户发送推送消息

使用GCM向所有用户发送推送消息
EN

Stack Overflow用户
提问于 2016-05-31 19:14:32
回答 1查看 416关注 0票数 0

我正试图向我的应用程序的所有android用户发送推送消息。我只能发送给一个用户,不能发送给所有人。

这是我的代码:

PUSH.php

代码语言:javascript
复制
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'my api key');

$registrationIds = array( 'user_push_id');


$msg = array
(
    'URL'   => 'http://www.apple.com/in/',
    'message'   => 'Did you like it ???',
    'image' =>'http://icons.iconarchive.com/icons/dan-wiersma/apple-tv/128/Apple-Logo-icon.png'
);

$fields = array
(
    'registration_ids'  => $registrationIds,
    'data'          => $msg
);

$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;
?>

请帮助我从mysql表中获取以下所有用户:

代码语言:javascript
复制
$sth = mysqli_query("SELECT PUSH_ME from push_msg_android");
$registrationIds = array();
while($r = mysqli_fetch_array($sth)) {
    $registrationIds['PUSH_ID'] = $r;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-31 19:22:20

为了向多个用户发送推送通知,需要将所有用户的注册In填充到registrationIds数组中。

最后,如果您有3个用户,它应该是这样的:

代码语言:javascript
复制
[78475826748590, 74648598273895, 85737485957849]

在您的例子中,它意味着替换这一行:

代码语言:javascript
复制
$registrationIds = array( 'user_push_id');

出自:

代码语言:javascript
复制
$result = mysqli_query($db, "SELECT PUSH_ID from push");
$registrationIds = array();
while($row = mysqli_fetch_array($result)) {
   $registrationIds[] = $row[0];
}

这是假设您的用户注册in存储在数据库的表PUSH_IDpush列中。

在更正常的设置中,用户的注册In将存储在表users中的列users中,查询应该是:

代码语言:javascript
复制
$result = mysqli_query($db, "SELECT push_notification_id from users");
$registrationIds = array();
while($row = mysqli_fetch_array($result)) {
   $registrationIds[] = $row[0];
}

您还需要在此之前创建$db对象,以创建到数据库的连接。例如:

代码语言:javascript
复制
$db = mysqli_connect("localhost","my_user","my_password","my_db");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37553885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档