【问题标题】:Send push notification to Android, with laravel-push-notification使用 laravel-push-notification 向 Android 发送推送通知
【发布时间】:2016-03-18 04:23:55
【问题描述】:

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

在我的 routes.php 中,我有:

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

TestGCM控制器:

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 返回:

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

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

【问题讨论】:

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


    【解决方案1】:

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

    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;
        }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多