使用 laravel-push-notification 向 Android 发送推送通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36076100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send push notification to Android, with laravel-push-notification
提问by kenpeter
A laravel-push-notification addon for laravel 5: https://github.com/davibennun/laravel-push-notification
laravel 5 的 laravel-push-notification 插件:https: //github.com/davibennun/laravel-push-notification
In my routes.php, I have:
在我的 routes.php 中,我有:
Route::get('/test_gcm', 'TestGCMController@index');
Route::get('/test_gcm', 'TestGCMController@index');
TestGCMController:
测试GCM控制器:
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);
}
}
so when I visit site.com/test_gcm
所以当我访问 site.com/test_gcm
I am not sure it is successfully or failure. There is no indication in the image.
我不确定它是成功还是失败。图像中没有任何指示。
The android app is from this tutorial: http://www.androidhive.info/2016/02/android-push-notifications-using-gcm-php-mysql-realtime-chat-app-part-1/, I am able to get the GCM token, so the app is not working, but I don't receive any notification from the Android phone in emulator.
android应用程序来自本教程:http: //www.androidhive.info/2016/02/android-push-notifications-using-gcm-php-mysql-realtime-chat-app-part-1/,我可以获取 GCM 令牌,因此该应用程序无法运行,但我在模拟器中没有收到来自 Android 手机的任何通知。
回答by Manoj Patel
You can use below code to send push notificaton on users mobile , hope it will helps you :
您可以使用以下代码在用户移动设备上发送推送通知,希望对您有所帮助:
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;
}