[英]FCM sending notification through fcm console to my ios device but from php service it is not sending the notification
FCM通过fcm控制台向我的ios设备发送通知,但是从php服务发送通知。 我想使用FCM将通知发送到我的应用程序。 我已经在php中实现了网络服务,以便将消息发送到我的应用服务器上的应用。 为此,我创建了4个服务。
创建组后,我成功获取了一个通知密钥,并使用fcm注册了一个注册ID。 当我使用通知键调用send_comments.php时,它会返回{{success“:1,” failure“:0}的json数据。 但是我的ios没有收到任何通知。 我已经正确实现了所有方法。 它与fcm控制台很好地一起工作,但与php服务一起却不能。 任何人都可以知道这一点。 我附上所有4个php文件。 请帮我。
group_create.php
<?php
$url = 'https://android.googleapis.com/gcm/notification';
$notification_key_name = $_REQUEST['notification_key_name'];
$regid = $_REQUEST['regid'];
$fields = array(
"operation"=>"create",
"notification_key_name"=>$notification_key_name,
"registration_ids"=> array($regid)
);
$fields = json_encode( $fields );
$headers = array (
"Authorization:key=A************************",
"Content-Type:application/json",
"project_id:78508******"
);
$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_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
//echo $result;
$res_notification_key = json_decode($result,true);
if(array_key_exists('notification_key', $res_notification_key)){
$notification_key = $res_notification_key['notification_key'];
echo $notification_key;
}
else{
echo $result;
}
curl_close ( $ch );
?>
device_add.php
<?php
$senderId = "785********";
$notification_key_name= $_REQUEST['notification_key_name'];
$reg_id = $_REQUEST['regid'];
$notification_key = $_REQUEST['not_key'];
$apiKey =
$url = 'https://android.googleapis.com/gcm/notification';
$headers = array (
"Accept:application/json",
"Authorization:key=A******************",
"Content-Type:application/json",
"project_id:78508*****"
);
$fields = array(
"operation"=>"add",
"notification_key_name"=> $notification_key_name,
"registration_ids"=> array($reg_id),
"notification_key"=>$notification_key
);
$fields = json_encode( $fields );
$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_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
echo $result;
$res_notification_key = json_decode($result,true);
if(array_key_exists('notification_key', $res_notification_key)){
$notification_key = $res_notification_key['notification_key'];
echo $notification_key;
}
else{
echo $result;
}
curl_close ( $ch );
?>
device_remove.php
<?php
$senderId = "78508*****";
$notification_key_name= $_REQUEST['notification_key_name'];
$reg_id = $_REQUEST['regid'];
$notification_key = $_REQUEST['not_key'];
$apiKey =
$url = 'https://android.googleapis.com/gcm/notification';
$headers = array (
"Accept:application/json",
"Authorization:key=A***********",
"Content-Type:application/json",
"project_id:78508*****"
);
$fields = array(
"operation"=>"remove",
"notification_key_name"=> $notification_key_name,
"registration_ids"=> array($reg_id),
"notification_key"=>$notification_key
);
$fields = json_encode( $fields );
$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_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
echo $result;
$res_notification_key = json_decode($result,true);
if(array_key_exists('notification_key', $res_notification_key)){
$notification_key = $res_notification_key['notification_key'];
echo $notification_key;
}
else{
echo $result;
}
curl_close ( $ch );
?>
send_comments.php
<?php
$senderId = "78508*****";
$notification_key = $_REQUEST['not_key'];
$url = 'https://fcm.googleapis.com/fcm/send';
$headers = array (
"Authorization:key=A*****************",
"Content-Type:application/json",
);
$msg = array("hello"=>"This is a Firebase Cloud Messaging Device Group Message!");
$msg_dict = json_encode($msg);
//echo $msg_dict;
$fields = array(
"to"=>$notification_key,
"data"=>array(
"message" => "hell",
),
);
$fields = json_encode( $fields );
$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_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
echo $result;
$res_notification_key = json_decode($result,true);
curl_close ( $ch );
?>
APN遵循以下格式
{"aps":{"alert":"Testing.. (0)","badge":1,"sound":"default"}}
即
{
"aps":{
"alert":"message here"
}
}
如果从服务器端发送的格式是此格式,则只有iPhone将显示通知,否则,明智的通知数据将仅通过以下方式在控制台中打印
- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
// Print full message
NSLog(@"Notification :%@", remoteMessage.appData);
}
但不会显示在通知列表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.