使用laravel-push-notification

时间:2016-03-18 04:23:56

标签: php android laravel notifications google-cloud-messaging

laravel 5的laravel-push-notification插件:https://github.com/davibennun/laravel-push-notification

在我的routes.php中,我有:

  

路由:: get(' / test_gcm',' TestGCMController @ index');

TestGCMController:     

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Davibennun\LaravelPushNotification\Facades\PushNotification;

use App\Http\Requests;

class TestGCMController extends Controller
{
  public function index()
  {
    // Change it when device launching sometimes
    $deviceToken = "12345";

    $return = PushNotification::app('appNameAndroid')
      ->to($deviceToken)
      ->send('Hello World, i`m a push message');

    var_dump($return);
  }
}

所以当我访问site.com/test_gcm

var_dump返回: enter image description here

我不确定它是成功还是失败。图像中没有任何迹象。

Android应用程序来自本教程:http://www.androidhive.info/2016/02/android-push-notifications-using-gcm-php-mysql-realtime-chat-app-part-1/,我可以获取GCM令牌,因此应用程序无法正常工作,但我无法在模拟器中收到来自Android手机的任何通知。

1 个答案:

答案 0 :(得分:2)

您可以使用以下代码在移动用户上发送推送通知,希望它将对您有所帮助:

function sendPushNotification($fcm_token, $title, $message, $id="") {  
        $push_notification_key = Config::get('settings.PUSH_NOTIFICATION_KEY');    
        $url = "https://fcm.googleapis.com/fcm/send";            
        $header = array("authorization: key=" . $push_notification_key . "",
            "content-type: application/json"
        );    

        $postdata = '{
            "to" : "' . $fcm_token . '",
                "notification" : {
                    "title":"' . $title . '",
                    "text" : "' . $message . '"
                },
            "data" : {
                "id" : "'.$id.'",
                "title":"' . $title . '",
                "description" : "' . $message . '",
                "text" : "' . $message . '",
                "is_read": 0
              }
        }';

        $ch = curl_init();
        $timeout = 120;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

        // Get URL content
        $result = curl_exec($ch);    
        // close handle to release resources
        curl_close($ch);

        return $result;
    }