php - 使用 php 发送 fcm 通知和消息
问题描述
我正在努力使用 react-native-firebase 实现通知
onNotification 函数根本没有被调用,所以我几乎疯了。
但我认为这可能是服务器问题。
所以我要求我们的 php 后端开发人员给我与 fcm 通知相关的服务器代码。
我认为他似乎只发送消息而不是通知。
如果是对的,我怎么能告诉他修复这个功能。如果不是,我不知道该怎么办了..
请帮助我了解这方面的知识!
谢谢你们!
function sendFCM($notif_array, $id) {
$API_KEY = "api_key";
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' => array (
$id
),
// 'data' => array (
// "message" => $message,
// "type" => $notif_type
// )
'content_available'=>true,
'priority'=>'high',
'data' => $notif_array
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . $API_KEY,
'Content-Type: application/json'
);
$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;
curl_close ( $ch );
}
解决方案
您应该在 php 端添加此代码:
function sendFCM($notif_array, $id) {
$API_KEY = "api_key";
$url = 'https://fcm.googleapis.com/fcm/send';
$message = [
'body' => 'Hello, This is a notification.',
'title' => 'Your Title',
'notification_type' => 'Test'
];
$notification = [
'body' => 'Hello, This is a notification.',
'title' => 'Your Title',
];
$fields = array (
'registration_ids' => array (
$id
),
'notification' => $notification,
'data' => $message,
'priority' => 'high',
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . $API_KEY,
'Content-Type: application/json'
);
$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 );
curl_close ( $ch );
return 'success';
}
对于IOS:
function iospush( $id ) {
$msg = 'Test notification';
$host = 'gateway.push.apple.com'; /Here is ecample */
$passphrase = YourIOSpassphrase;
$ios_notifiaction_certificate = '/add full path where ios certiticate stay';
try {
$streamContext = stream_context_create();
stream_context_set_option( $streamContext, 'ssl', 'local_cert', $ios_notifiaction_certificate );
stream_context_set_option( $streamContext, 'ssl', 'passphrase', $passphrase);
$apns = stream_socket_client( 'ssl://'.$host, $error, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $streamContext );
$payload[ 'aps' ] = array( 'alert' => $msg, 'badge' => '0', 'sound' => 'default', 'notification_type' => 'Test' );
$payload = json_encode( $payload );
$apnsMessage = chr(0) . pack( 'n', 32 ) . pack( 'H*', $id ) . pack( 'n', strlen( $payload ) ) . $payload;
$fwriteRes = fwrite( $apns, $apnsMessage, strlen( $apnsMessage ) );
fclose( $apns );
return 'Success';
} catch( Exception $e ) {
return true;
// return $e->getMessage();
}
}
推荐阅读
- c# - “/”应用程序中的服务器错误。发布网站后系统找不到文件指定错误
- python - 树莓派:pip 提供不兼容的轮子,安装 pip3
- javascript - html5sortable拖放不适用于ajax加载HTML
- flutter - Flutter 平台特定的导航方法
- javascript - 如何使用 new Date() 获取 DD-MM-YY hh:mm a, en-US, +0100 格式
- postgresql - 数据库初始化发生在 docker-compose 中其他服务的启动之后
- python - 实现我制作成视频的 CNN+LSTM 模型时遇到问题
- c# - 有没有办法反序列化为 ExpandoObject 并在之后进行强类型化
- python - 如何隐藏 print() 或在某些情况下不执行
- android - React Native:错误无法安装应用程序。确保您已设置 Android 开发环境