2017-07-21 21 views
3

firebaseを使用してIonic 2アプリに通知をプッシュしたいとします。私はfirebaseコンソールを使って直接通知をプッシュすることができますが、私はPHPファイル経由で送信したいと思います。ionic - PHPを使用したfirebase通知が機能しない

私は私のようにPHPからの応答を取得送る:{"message_id":5718309985299480645}

と電話には通知がありません。

私はapp.component.tsコンストラクタにthis.fcm.subscribeToTopic('all')を配置しました。私は私が間違っているのか知ってはいけない...

this.fcm.subscribeToTopic('all')

は私のアプリでは、FCMに関連したコードのみです。

MY PHPコード:

<?php 

    $data = array('title'=>'FCM Push Notifications'); 
    $target = '/topics/mytopic'; 


    //FCM API end-point 
    $url = 'https://fcm.googleapis.com/fcm/send'; 
    //api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key 
    $server_key = 'my_server_key_from_firebase'; 

    $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 
    ); 
    //CURL request to route notification to FCM connection server (provided by Google)   
    $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('Oops! FCM Send Error: ' . curl_error($ch)); 
    } 
    curl_close($ch); 
    echo $result; 

?> 

私はEVEN PUSHBOTS試みたが、彼らが使用beeingている特別な場合には、

+1

あなたはこのページの最後に似ペイロード以下の通りです?:https://github.com/fechanique/cordova-plugin-fcm –

+0

FCM_PLUGIN_ACTIVITYは何ですか? –

+0

これは、FCMのAndroidでプッシュを使用するためのデフォルトであるため、これを探すのは面倒ではありませんが、ペイロードの説明に「どのように動作するか」と書かれているように、 –

答えて

2

私はfcm docsを見て問題を解決しました。

私はにPHPファイルを変更:、私はあなたが優先順位やclick_actionの設定が表示されない

$data = array('title'=>'Mukta Arjun Jagtap', 'body'=>$msg, 'sound'=>'default'); 
    $target = '/topics/notetoall'; 

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

    $server_key = 'my_server_api_key_from_firebase'; 

    $fields = array(); 
    $fields['notification'] = $data; // <-- this is what I changed from $fields['body'] 
    if(is_array($target)){ 
    $fields['registration_ids'] = $target; 
    }else{ 
    $fields['to'] = $target; 
    } 
1

すべてのプラグインが用意デバイスの後に呼び出されなければならないPHPでデバイスON NOTFICATIONSを取得できませんでしたapp.components(一部のページでは、最初のものではなく、アプリケーションが既に準備されていてプラグインがロードされているので、コンストラクタ内で使用できます)。

ので

準備デバイス上のトピックをサブスクライブ
this.platform.ready().then(() => { 
    this.fcm.subscribeToTopic('all') 
}); 

は、この情報がお役に立てば幸いです。

+0

ありがとう、私はこのソリューションを試しました、これは私がFirebaseのコンソールから送信する場合は動作しますが、PHPからは動作しません。私はPHPコードを貼り付けています。確認してください。 –

+0

これは別のものですが、あなたの質問はFCMプラグインコールをどこに置くかということでした。あなたが編集して以来、私は今すぐ解決された –

+0

を見ています。問題はPHPファイル内にあるようです。 –

関連する問題