亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 GCM 遷移到 FCM

從 GCM 遷移到 FCM

PHP
海綿寶寶撒 2022-07-16 16:16:22
最近我從 GCM 遷移到 FCM,過去幾天我一直在努力讓它工作。Android 應用程序正在接收來自 google firebase 控制臺的通知,但它們沒有來自 php 服務器。這是我的 PHP 服務器端代碼:<?phpdefine("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();}@Overridepublic 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());}}
查看完整描述

3 回答

?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

嘗試使用這個:


@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)

        .setContentText("Heading")

        .setContentTitle("subheading")

        .setContentIntent(pendingIntent)

        .addAction(android.R.drawable.sym_action_chat,"Title",pendingIntent)

        .setChannelId(CHANNEL_ID)

        .setSmallIcon(android.R.drawable.sym_action_chat)

        .build();


NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.createNotificationChannel(notificationChannel);

notificationManager.notify(1,notification);



 }


查看完整回答
反對 回復 2022-07-16
?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

要從 php 服務器發送Firebase 推送通知,您可以使用此代碼。


<?php


function sendMessage($data, $target, $serverKey){

    //FCM api URL

    $rsp = [];

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

    //api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key

    $server_key = $serverKey;

    $fields = array();

    $fields['data'] = $data;

    if(is_array($target)){

            $fields['registration_ids'] = $target;

        }else{

            $fields['to'] = $target;

    }

    //header with content_type api key

    $headers = array(

        'Content-Type:application/json',

        'Authorization:key='.$server_key

    );


    $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('FCM Send Error: ' . curl_error($ch));

    }

    curl_close($ch);


    //print_r($result);

    return $result;

}

如果你想更新你的 android 代碼,你可以關注這篇文章:Firebase 推送通知


查看完整回答
反對 回復 2022-07-16
?
哆啦的時光機

TA貢獻1779條經驗 獲得超6個贊

你得到錯誤的數據。它應該是這樣的?;驀L試先打印數據然后從數組中獲取。

sendNotification(remoteMessage.getData().get("message"));
Log.d("Push data", remoteMessage.getData().toString());


查看完整回答
反對 回復 2022-07-16
  • 3 回答
  • 0 關注
  • 115 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號