简体   繁体   English

使用laravel-push-notification将推送通知发送到Android

[英]Send push notification to Android, with laravel-push-notification

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'); 路线:: get('/ test_gcm','TestGCMController @ index');

TestGCMController: 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);
  }
}

so when I visit site.com/test_gcm 所以当我访问site.com/test_gcm

The var_dump returns: var_dump返回: 在此处输入图片说明

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手机收到任何通知。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM