编程语言
首页 > 编程语言> > ionic-使用PHP的Firebase通知不起作用

ionic-使用PHP的Firebase通知不起作用

作者:互联网

我想使用Firebase将通知推送到我的Ionic 2应用程序.我可以使用Firebase控制台直接推送通知,但是我想通过php文件发送通知.

当我发送邮件时,我收到来自PHP的响应:{“ message_id”:5718309985299480645}

并且电话中没有通知.

我已将this.fcm.subscribeToTopic(‘all’)放置在app.component.ts构造函数中.

我不知道我在做什么错..

this.fcm.subscribeToTopic(‘all’)是我应用中唯一与fcm相关的代码.

我的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;

?>

我什至尝试过PUSHBOTS,但无法使用PHP在设备上获得通知

解决方法:

我通过查看fcm docs解决了这个问题.

我将PHP文件更改为:

   $data = array('title'=>'Title of the notification', '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;
   }

标签:firebase,ionic-framework,ionic2,php,cordova-plugin-fcm
来源: https://codeday.me/bug/20191025/1931669.html