我正在尝试设置服务器以发送推送通知。
我正在尝试向指定包的用户发送推送通知,例如像com.mysite.android
之类的东西(此选项在Firebase控制台中可用)。
检查this之类的答案我无法理解如何设置to
参数以向指定包的用户发送推送通知。我可以找到向specific devices by their ids
或news topics
发送通知的示例。
澄清:我是应用程序的所有者,我希望发送推送通知。
如果有帮助,这是我的代码:
<?php
/*
Parameter Example
$data = array('post_id'=>'12345','post_title'=>'A Blog post');
$target = 'single tocken id or topic name';
or
$target = array('token1','token2','...'); // up to 1000 in one request
*/
echo "Start<br/>";
$result = sendMessage('{"id":"hello"}',null);
var_dump($result);
function sendMessage($data,$target){
//FCM api URL
$url = 'https://fcm.googleapis.com/fcm/send';
//api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
$server_key = 'xxxxxxxxxxxxxxxxxxxkeyxxxxxxxxxxxxxxxxxxxxxxxx';
$fields = array();
$fields['data'] = $data;
$fields['restricted_package_name']='com.mysite.android ';
$fields['dry_run']=true;
if (isset($target)){
if(is_array($target)){
$fields['registration_ids'] = $target;
}else{
$fields['to'] = $target;
}
}
//header with content_type api key
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$server_key
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
?>
注意:我故意删除了server_key
。
答案 0 :(得分:0)
除了您拥有的应用程序之外,您无法向任何外部应用程序发送推送通知,只需将用户设备令牌和服务器密钥链接到一个项目下,因此您必须使用服务器密钥和有效的设备令牌来获取“X”应用程序
答案 1 :(得分:0)
这是可以管理的。但是,为了能够向应用程序发送通知,您首先需要成为有效的发件人。
通常情况下(如果您是应用所有者),您只需将应用添加到Firebase项目,生成google-services.json文件并将其添加到Android项目中。
如果您不是应用程序所有者,并且您不想通过Firebase项目添加应用程序,只需修改应用程序中使用的google-services.json,将SenderID添加到它。但我非常怀疑你是否有权首先修改一个不属于你的应用程序。
注意:不建议您修改google-services.json。
如果您能够将您的项目设置为有效发件人,则必须使用Topic Messaging并将设备订阅为其应用程序包名称的主题名称。
然后在发送到主题时,只需在to
参数中使用包的名称,如下所示:
"to": "/topics/<app_package_name_here>"