使用FCM和php推送通知
问题描述:
我试图从我的反应本机应用程序发送推送通知使用fcm和php作为server.Following是我的代码在反应从服务器接收通知。使用FCM和php推送通知
pushNotification.js
import React, { Component } from "react";
import FCM from "react-native-fcm";
export default class PushNotification extends Component {
constructor(props) {
super(props);
}
componentDidMount() {
// this method generate fcm token.
FCM.requestPermissions();
FCM.getFCMToken().then(token => {
console.log("TOKEN (getFCMToken)", token);
});
// This method get all notification from server side.
FCM.getInitialNotification().then(notif => {
console.log("INITIAL NOTIFICATION", notif)
});
// This method give received notifications to mobile to display.
this.notificationUnsubscribe = FCM.on("notification", notif => {
console.log("a", notif);
if (notif && notif.local_notification) {
return;
}
this.sendRemote(notif);
});
// this method call when FCM token is update(FCM token update any time so will get updated token from this method)
this.refreshUnsubscribe = FCM.on("refreshToken", token => {
console.log("TOKEN (refreshUnsubscribe)", token);
this.props.onChangeToken(token);
});
}
// This method display the notification on mobile screen.
sendRemote(notif) {
console.log('send');
FCM.presentLocalNotification({
title: notif.title,
body: notif.body,
priority: "high",
click_action: notif.click_action,
show_in_foreground: true,
local: true
});
}
componentWillUnmount() {
this.refreshUnsubscribe();
this.notificationUnsubscribe();
}
render() {
return null;
}
}
我的PHP脚本是follows.Here我试图抓住每一个用户的设备令牌发送通知。
notification.php
<?php
include 'db.php';
$check_json = file_get_contents('php://input');
$obj= json_decode($check_json);
$uid =$obj->{'uuid'};
$fcm =$obj->{'fcm'}
$to =$fcm;
$data = array(
'title'=>"Testmessage",
'message'=>"You have a new request");
function send_message($to,$data){
$server_key=
'*******';
$target= $to;
$headers = array(
'Content-Type:application/json',
'Authorization:key=' .$server_key
);
$ch = curl_init();
$message = array(
'fcm' => $to,
'priority' => 'high',
'data' => $data
);
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://fcm.googleapis.com/fcm/send',
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode($message)
));
$response = curl_exec($ch);
}
curl_close($ch);
//echo $response;
return $response;
?>
我是一个真正的设备和emulator.Trying在测试这个从真实设备发送推送通知到仿真器(可能吗?),但不工作。任何人都可以请帮我,我是新来的反应,所以请..
答
它可以从物理设备发送推送通知到模拟器,但模拟器应与FCM
public function sendMessageThroughFCM($arr) {
//Google Firebase messaging FCM-API url
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = (array) $arr;
define("GOOGLE_API_KEY","XXXXXXXXXXXXXXXXXXXXX");
$headers = array(
'Authorization: key=' . GOOGLE_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_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('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
注册
诬陷阵列发送通知
$arr = array(
'registration_ids' => $androidTokens,
'notification' => array('title' => $notificationTitle, 'body' => $notificationBody),
'data' => array('title' => $notificationTitle, 'body' => $notificationBody)
);
推FCM名单令牌到你需要的设备,如果不是产生它发送通知
array_push($androidTokens,$token['Fcm_registration_token']);
刷新FCM令牌
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
通过API调用将refreshedToken
更新到服务器。
因此,如果我在两台物理设备上测试,那么它是否适用于我的上述代码? – anu
是的,它会工作。您应该触发接收器FCM注册ID的推送通知。例如:如果您尝试发送设备B的推送通知,则应在服务器的'registration_ids'数组项中添加设备B FCM注册令牌。 – Bethan
Fcm令牌不是在某些android设备上生成的。该怎么办?我如何刷新令牌? – anu