3
fcm 및 php를 server로 사용하여 반응하는 네이티브 앱에서 푸시 알림을 보내려고합니다. 내 코드가 서버에서 알림을 수신하는 반응을 보입니다.FCM 및 PHP를 사용하여 알림 푸시
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 같다
pushNotification.js 내가 각 사용자의 장치 토큰을 복용하여 통지를 보내도록 노력하고 있어요.
나는 (? 가능한 권리를) 에뮬레이터에 실제 장치에서 푸시 알림을 보내 겠다는 장치와 emulator.Trying에서이 테스트를 나누었다 작동하고 있지 않다
<?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;
?>
notification.php. 아무도
그렇다면 두 개의 물리적 장치에서 테스트 중이므로 위의 코드에서 작동합니까? – anu
예, 작동합니다. 수신자 FCM 등록 ID에 대한 푸시 알림을 트리거해야합니다. 예 : 기기 B에 푸시 알림을 보내려는 경우 기기의 B FCM 등록 토큰을 서버의 'registration_ids'배열 항목에 추가해야합니다. – Bethan
일부 안드로이드 장치에서 Fcm 토큰이 생성되지 않습니다. 어떻게해야합니까? 토큰을 새로 고치는 방법은 무엇입니까? – anu