3

fcmとphpをserverとして使用して、反応しているネイティブアプリからプッシュ通知を送信しようとしています。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ようです。

<?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

私は(?可能右)をエミュレータに実際のデバイスからのプッシュ通知を送信するために、実際のデバイスとemulator.Tryingでこれをテストし.But働いていません。誰もがme.Iはそう、してください反応する新しいです助けてください..

答えて

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

私は2つの物理的なデバイスでテストしている場合、それは私の上記のコードのために動作するのだろうか? – anu

+0

はい、うまくいきます。受信側FCM登録IDのプッシュ通知をトリガーする必要があります。例:デバイスBのプッシュ通知を送信しようとしている場合は、デバイスB FCM登録トークンをサーバーの 'registration_ids'配列項目に追加する必要があります。 – Bethan

+0

一部のアンドロイドデバイスでFcmトークンが生成されていません。どうすればいいですか?トークンを更新するにはどうすればよいですか? – anu

関連する問題