Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用FCM和php推送通知_Php_Android_Firebase Cloud Messaging_React Native Android - Fatal编程技术网

使用FCM和php推送通知

使用FCM和php推送通知,php,android,firebase-cloud-messaging,react-native-android,Php,Android,Firebase Cloud Messaging,React Native Android,我正在尝试使用fcm和php作为服务器从我的react原生应用程序发送推送通知 pushNotification.js import React, { Component } from "react"; import FCM from "react-native-fcm"; export default class PushNotification extends Component { constructor(props) { super(props); } comp

我正在尝试使用fcm和php作为服务器从我的react原生应用程序发送推送通知

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脚本如下。在这里,我试图通过获取每个用户的设备令牌来发送通知

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


我正在真实设备和模拟器上测试此功能。尝试将推送通知从真实设备发送到模拟器(可能是吧?)。但不起作用。请任何人帮助我。我是新反应者,所以请..

可以将推送通知从物理设备发送到模拟器,但模拟器应向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)
        );
array_push($androidTokens,$token['Fcm_registration_token']);
推送需要向哪个设备发送通知的FCM令牌列表

$arr = array(
            'registration_ids' => $androidTokens,
            'notification' => array( 'title' => $notificationTitle, 'body' => $notificationBody),
            'data' => array( 'title' => $notificationTitle, 'body' => $notificationBody)
        );
array_push($androidTokens,$token['Fcm_registration_token']);
如果未生成FCM令牌,请刷新该令牌

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

通过API调用将
refreshedToken
更新到服务器。

因此,如果我在两个物理设备上进行测试,那么它对我的上述代码是否有效?是的,它会有效。您应该为接收方FCM注册ID触发推送通知。例如:如果您试图为设备B发送推送通知,那么您应该在服务器上的
注册ID
数组项中添加设备B FCM注册令牌。某些android设备中没有生成FCM令牌。要做什么?如何刷新令牌?现在我已经完成了在react中编写了fcm令牌的代码。那么如何在android中编写生成fcm令牌的代码呢?我不太确定react,但我可以帮助您提供更有用的参考