从 GCM 迁移到 FCM

最近我从 GCM 迁移到 FCM,过去几天我一直在努力让它工作。


Android 应用程序正在接收来自 google firebase 控制台的通知,但它们没有来自 php 服务器。


这是我的 PHP 服务器端代码:


<?php




define("GOOGLE_API_KEY", Setting::get('browser_key') ? Setting::get('browser_key') : "");



class GCM {


function __construct() {


}



public function send_notification($registatoin_ids, $message) {


    Log::info("GOOGLE_API_KEY".GOOGLE_API_KEY);


    include_once 'const.php';

    $url = 'https://fcm.googleapis.com/fcm/send';


    $fields = array(

        'registration_ids' => $registatoin_ids,

        'data' => $message,

    );


    $headers = array(

        'Authorization: key=' . GOOGLE_API_KEY,

        'Content-Type: application/json'

    );


    Log::info('***** PUSH MESSAGE ******'.print_r(json_encode($fields),true));


    $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_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));


    $result = curl_exec($ch);


    Log::info(print_r($result,true));


    if ($result === FALSE) {

        //die('Curl failed: ' . curl_error($ch));

        Log::error('Curl failed: ' . curl_error($ch));

    }

    else{

        //echo $result;

        Log::error($result);

    }


    // Close connection

    /*curl_close($ch);

     echo $result/*."\n\n".json_encode($fields); */


}


}

?>

这是我的 const.php


<?php


    define('TEAM','team');


    define('MESSAGE' , 'message');


?>

这是我的 Firebase 消息传递代码:


public class MessagingService extends FirebaseMessagingService {


private static final String TAG = "FCM Message";


public MessagingService() {

    super();

}


@Override

public void onMessageReceived(RemoteMessage remoteMessage) {

    super.onMessageReceived(remoteMessage);


    sendNotification(remoteMessage.getNotification().getBody());


    Log.d(TAG, "From: " + remoteMessage.getFrom());

    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification());

}

}


海绵宝宝撒
浏览 89回答 3
3回答

宝慕林4294392

尝试使用这个:@TargetApi(Build.VERSION_CODES.O)@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)public void show_Notification(){Intent intent=new Intent(getApplicationContext(),MainActivity.class);String CHANNEL_ID="MYCHANNEL";NotificationChannel notificationChannel=new NotificationChannel(CHANNEL_ID,"name",NotificationManager.IMPORTANCE_LOW);PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent,0);Notification notification=new Notification.Builder(getApplicationContext(),CHANNEL_ID)&nbsp; &nbsp; &nbsp; &nbsp; .setContentText("Heading")&nbsp; &nbsp; &nbsp; &nbsp; .setContentTitle("subheading")&nbsp; &nbsp; &nbsp; &nbsp; .setContentIntent(pendingIntent)&nbsp; &nbsp; &nbsp; &nbsp; .addAction(android.R.drawable.sym_action_chat,"Title",pendingIntent)&nbsp; &nbsp; &nbsp; &nbsp; .setChannelId(CHANNEL_ID)&nbsp; &nbsp; &nbsp; &nbsp; .setSmallIcon(android.R.drawable.sym_action_chat)&nbsp; &nbsp; &nbsp; &nbsp; .build();NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);&nbsp; &nbsp; notificationManager.createNotificationChannel(notificationChannel);notificationManager.notify(1,notification);&nbsp;}

拉风的咖菲猫

要从 php 服务器发送Firebase 推送通知,您可以使用此代码。<?phpfunction sendMessage($data, $target, $serverKey){&nbsp; &nbsp; //FCM api URL&nbsp; &nbsp; $rsp = [];&nbsp; &nbsp; $url = 'https://fcm.googleapis.com/fcm/send';&nbsp; &nbsp; //api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key&nbsp; &nbsp; $server_key = $serverKey;&nbsp; &nbsp; $fields = array();&nbsp; &nbsp; $fields['data'] = $data;&nbsp; &nbsp; if(is_array($target)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fields['registration_ids'] = $target;&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fields['to'] = $target;&nbsp; &nbsp; }&nbsp; &nbsp; //header with content_type api key&nbsp; &nbsp; $headers = array(&nbsp; &nbsp; &nbsp; &nbsp; 'Content-Type:application/json',&nbsp; &nbsp; &nbsp; &nbsp; 'Authorization:key='.$server_key&nbsp; &nbsp; );&nbsp; &nbsp; $ch = curl_init();&nbsp; &nbsp; curl_setopt($ch, CURLOPT_URL, $url);&nbsp; &nbsp; curl_setopt($ch, CURLOPT_POST, true);&nbsp; &nbsp; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);&nbsp; &nbsp; curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);&nbsp; &nbsp; curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);&nbsp; &nbsp; curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);&nbsp; &nbsp; curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));&nbsp; &nbsp; $result = curl_exec($ch);&nbsp; &nbsp; if ($result === FALSE) {&nbsp; &nbsp; &nbsp; &nbsp; //die('FCM Send Error: ' . curl_error($ch));&nbsp; &nbsp; }&nbsp; &nbsp; curl_close($ch);&nbsp; &nbsp; //print_r($result);&nbsp; &nbsp; return $result;}如果你想更新你的 android 代码,你可以关注这篇文章:Firebase 推送通知

哆啦的时光机

你得到错误的数据。它应该是这样的。或尝试先打印数据然后从数组中获取。sendNotification(remoteMessage.getData().get("message")); Log.d("Push&nbsp;data",&nbsp;remoteMessage.getData().toString());
打开App,查看更多内容
随时随地看视频慕课网APP