我可以从Firebase控制台发送通知,但我无法通过php发送

时间:2017-12-05 21:54:32

标签: php firebase notifications firebase-cloud-messaging

服务器密钥为true 我已经尝试单独使用旧服务器密钥和新服务器的密钥,但它没有发生。 我正在使用来自Firebase控制台的令牌向单个设备发送通知,它可以正常工作。 我将它发送到所有设备,它可以工作。

当我用php发送时,通知不会出现在模拟器和手机上

php显示了这个结果 卷曲结果: “成功”:3 “失败”:0 “canonical_ids”:0"

问题出在哪里?

FcmMessasingService

package com.package.name;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.google.firebase.messaging.RemoteMessage;

public class FcmMessagingService extends com.google.firebase.messaging.FirebaseMessagingService{

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        showNotification(remoteMessage.getData().get("message"));
    }

    private void showNotification(String message) {

        Intent i = new Intent(this,MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setAutoCancel(true)
                .setContentTitle("FCM")
                .setContentText(message)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pendingIntent);

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        manager.notify(0,builder.build());
    }


}

php文件

<?php

	function send_notification ($tokens, $message)
	{

		$url = 'https://fcm.googleapis.com/fcm/send';
		$fields = array(
			 'registration_ids' => $tokens,
			 'data' => $message
			);

		$headers = array(
			'Authorization:key = AAAA7hSYiy0:APA91bHxEnClm7FnCHFF72N9YraNUatKv1n4UsZwoNH6AWJ_n7O4Ixbmq71jFVJ_6vLC_q4Aq_AMh4O_qGYxbp35XL7vzRyD7B6JUK9bWSBNQYhQ-SxZzJ5_7URJEmyJc2Y4fGmzK-pi',
			'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;
	}


	$conn = mysqli_connect("localhost","root","","token_table");
  $sql = " Select token from token_table";

	$result = mysqli_query($conn,$sql);
	$tokens = array();

	if(mysqli_num_rows($result) > 0 ){

		while ($row = mysqli_fetch_assoc($result)) {
			$tokens[] = $row["token"];
		}
	}

	mysqli_close($conn);

	$message = array("message" => "THIS IS TEST MESSAGE");
	$message_status = send_notification($tokens, $message);
	echo $message_status;

print_r($tokens);

 ?>

返回$ result和$ tokens

{"multicast_id":8903891154922407840,"success":3,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1512509834754831%01f815b6f9fd7ecd"},{"message_id":"0:1512509834755366%01f815b6f9fd7ecd"},{"message_id":"0:1512509834754833%01f815b6f9fd7ecd"}]}Array ( [0] => cWdBtAg-0ok:APA91bEeXBTdJqDXvYOrVR-ZacwCGzDDCVhLodfNjw-D9_YSxMSXbZNixZNAE7oZsCnFGguECytcNZWPMujiUj_A6BExccCo2U9-nuURB-PME4L27pOceNBhq52DLkU7JqcP4yMjWCnQ [1] => fzvOmSJDvwE:APA91bEWqamnklzY0QvRNDz9fI0CJehpwVAHmIdBFXovDMpmJqr5DEm3bIYdIALlwb2u53wuMtOMf2TdTQTj5LTlAIpeFnbnmXK7Q6f_j-k2mEcduwdRvjs2nQ5yw-TwkKY1HNfIy6jb [2] => dtMH2ehN-Bg:APA91bF6Zr9wYlWArvsjs-bu8c275HfaijbNz07prTWBaT8CYqclaaIjbXRq4tpWvqI8IdGIZomu3QjtjYOsbyFpPZiAoG2mX6fbadmRstgbE8-dVtdOW3RNblYLW3m1lkmcYIEMGI-q )

0 个答案:

没有答案