1

A laravel-push-notification addon for laravel 5: https://github.com/davibennun/laravel-push-notification

In my routes.php, I have:

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

so when I visit site.com/test_gcm

The var_dump returns: enter image description here

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.

1 Answer 1

2

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;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

where should push_notification_key come from?
push_notification_key is our project id Please check here my comments stackoverflow.com/questions/54017664/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.