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. 아무도

답변

1

그것은 에뮬레이터에 물리적 장치에서 푸시 알림을 전송하는 것이 가능하지만 에뮬레이터가 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; 
} 
에 등록해야합니다 .. 나랑 그렇게하시기 바랍니다 반응 새로운 오전 도와주세요 수 FCM의 목록이 토큰 통지를

$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']); 

를 전송해야하는 장치에

String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 

refreshedToken을 API 호출을 통해 서버로 업데이트하십시오.

+0

그렇다면 두 개의 물리적 장치에서 테스트 중이므로 위의 코드에서 작동합니까? – anu

+0

예, 작동합니다. 수신자 FCM 등록 ID에 대한 푸시 알림을 트리거해야합니다. 예 : 기기 B에 푸시 알림을 보내려는 경우 기기의 B FCM 등록 토큰을 서버의 'registration_ids'배열 항목에 추가해야합니다. – Bethan

+0

일부 안드로이드 장치에서 Fcm 토큰이 생성되지 않습니다. 어떻게해야합니까? 토큰을 새로 고치는 방법은 무엇입니까? – anu

관련 문제