FCM 通过 fcm 控制台向我的 ios 设备发送通知,但从 php 服务它没有发送通知

Posted

技术标签:

【中文标题】FCM 通过 fcm 控制台向我的 ios 设备发送通知,但从 php 服务它没有发送通知【英文标题】:FCM sending notification through fcm console to my ios device but from php service it is not sending the notification 【发布时间】:2016-08-26 09:06:57 【问题描述】:

FCM 通过 fcm 控制台向我的 ios 设备发送通知,但从 php 服务它不发送通知。我想使用 FCM 向我的应用发送通知。我已经在 php 中实现了 Web 服务,以向我的应用服务器上的应用发送消息。为此,我创建了 4 个服务。

    group_create.php device_add.php device_remove.php send_cmets.php

创建组后,我成功获得了通知密钥并使用 fcm 注册了注册 ID。当我使用通知键调用 send_cmets.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_cmets.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 );
?>

【问题讨论】:

查看***.com/questions/38479668/… 【参考方案1】:

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);

但不显示在通知列表中。

【讨论】:

以上是关于FCM 通过 fcm 控制台向我的 ios 设备发送通知,但从 php 服务它没有发送通知的主要内容,如果未能解决你的问题,请参考以下文章

FCM - 从 Java 向我的 android 设备发送通知

注册令牌不是有效的 FCM 注册令牌

Flutter FCM Ios 未在设备上显示

FCM iOS 项目未收到通知 - FCM 控制台显示成功但设备未收到

iOS 上的 Flutter FCM 不调用 onMessage 和 onResume

通过 FCM 发送推送通知时何时使用 iOS 设备令牌?