Laravel Push Notifications, non static method called statically - php

I am trying to implement this library for sending push notifications to iOS app. All my configurations are fine. When I tested the code snippet available at this page like this:
PushNotification::app('appNameIOS')
->to($deviceToken)
->send('Hello World, i`m a push message');
It threw this error:
Non-static method
Davibennun\LaravelPushNotification\PushNotification::Message() should
not be called statically
Rightly so, because when I opened the class, there was no such static method. There is one but that is not static. What am I doing wrong? Any help?
Edit 1
I have generated config file:
return array(
'hasalty_ios' => array(
'environment' =>'development',
'certificate' =>base_path('pem.p12'),
'passPhrase' =>'',
'service' =>'apns'
),
'hasalty_android' => array(
'environment' =>'production',
'apiKey' =>'yourAPIKey',
'service' =>'gcm'
)
);
Edit 2
My Laravel version is 5.5.31.

If you configure the library correctly, you should use
use Pushnotification;
instead of
use Davibennun\LaravelPushNotification\PushNotification;
When a user references any static method on the Cache facade, Laravel resolves the cache binding from the service container and runs the requested method (in this case, get) against that object.
How Facades Work
Edit
You must generate the config file before you use it:
php artisan vendor:publish --provider="Davibennun\LaravelPushNotification\LaravelPushNotificationServiceProvider" --tag="config"

Try to use this code:
Change here use Pushnotification instead
of use Davibennun\LaravelPushNotification\PushNotification;

Related

Download Cloudinary data from EC2

I use PHP Symfony for my backend. I need to download cloudinary data into my EC2 instance using the cloudinary url of a single file or folder. For that, i wanted to create a PHP script to do that.
I use cloudinary_php package (https://packagist.org/packages/cloudinary/cloudinary_php). I use the upload functionality to store my data, but it doesn't seem to have a download method.
I wanted to know if this is possible, with cloudinary_php or with another pachage ?
Thanks for your help!
In your Symfony's project root folder, you should have the file composer.json. Then add following entry that looks like this:
{
"require": {
"cloudinary/cloudinary_php": "^2"
}
}
Then make sure to install the dependencies. Follow this composer doc. After that, you can now then plug in your API key and Secret then instantiate a Cloudinary object:
require 'vendor/autoload.php';
use Cloudinary\Configuration\Configuration;
use Cloudinary\Api\Upload\UploadApi;
// configure globally via a JSON object
Configuration::instance([
'cloud' => [
'cloud_name' => 'your-cloud-name-here',
'api_key' => 'xxxxxxxx',
'api_secret' => 'xxxxxxxxxx'
],
'url' => [
'secure' => true
]
]);
//Instanstiate and generate an archive
$cloudinary = (new UploadApi());
$response = $cloudinary->createZip([
'tags' => 'jeep', // Change this base on your use case
'resource_type' => 'image' // Change this base on your use case
]);
//Check the response object
print_r($response);
//Make your own implementation here to download the archive.
The response object above should have the secure_url key where you can directly download the generated archive link. Visit the documentation here for more info. There are also lots of optional parameters you can pass and you should choose what works best for you. You should also consider Symfony's security best practices when referencing sensitive information. For general Cloudinary PHP SDK integration, visit this.

How to over ride a Laravel class with my own?

So I am working with Laravel 5.2 and I trying to shift to SQS as my queue serice.
There was a bug in 5.2 in QueueSqs.php which was fixed here -> https://github.com/illuminate/queue/blob/5.6/SqsQueue.php in 5.6
Now I am not sure I can upgrade to 5.6 yet, because a lot of things are working with 5.2 and I don't want to break anything.
But I am sure I can somehow use this class in my code from 5.6 and tell Laravel to use it somehow. But I don't know how to.
I haven't checked if this worked back in Laravel 5 or in class extends, but in newer versions Laravel generally tries to resolve the class from its container and by binding the original FQNS of the class to your your custom SqsQueue, it instead returns yours everytime the original is used:
class AppServiceProvider extend ServiceProvider
{
public function register()
{
$this->app->bind(\Illuminate\Queue\SqsQueue::class, function ($app) {
return new CustomSqsQueue();
});
}
}
Well, actually, instead of overriding the original class and re-binding it for Laravel to use you could also add your own custom queue connector and use this instead of Laravels native SQS queue connector:
To add a new connector for the queue to use, open up a service provider and add the following code to the boot
public function register()
{
$manager = $this->app['queue'];
$manager->addConnector('sqs-custom', function() {
return new CustomSqsQueue;
});
}
Then, in the queue.php config, add a new connection based on the original SQS connection, but with the driver name you chose in the first parameter or addConnector in the service provider. The key of the array ist the name of the connection you use to define your queues:
'sqs-custom' => [
'driver' => 'sqs-custom',
'key' => 'your-public-key',
'secret' => 'your-secret-key',
'prefix' => 'https://sqs.us-east-1.amazonaws.com/your-account-id',
'queue' => 'your-queue-name',
'region' => 'us-east-1',
],
Again, this is untested but should be able to work in Laravel 5.2.

Unable to use Laravel / Socialite with Lumen

I try to use the package Laravel\Socialite in my system in Lumen (5.1)
I added this in the config\services.php file :
<?php
//Socialite
'facebook' => [
'client_id' => '##################',
'client_secret' => '##################',
'redirect' => 'http://local.dev/admin/facebook/callback',
],
In bootstrap\app.php file :
class_alias(Laravel\Socialite\Facades\Socialite::class, 'Socialite');
$app->register(Laravel\Socialite\SocialiteServiceProvider::class);
Then I created a controller for the facebook authentication :
<?php
namespace App\Http\Controllers\Facebook;
use App\Http\Controllers\Controller;
use Laravel\Socialite\Contracts\Factory as Socialite;
class FacebookController extends Controller
{
public function redirectToProviderAdmin()
{
return Socialite::driver('facebook')->scopes(['manage_pages', 'publish_actions'])->redirect();
}
public function handleProviderCallbackAdmin()
{
$user = Socialite::driver('facebook')->user();
}
}
And in the routes.php :
$app->get('/admin/facebook/login', 'App\Http\Controllers\Facebook\FacebookController#redirectToProviderAdmin');
$app->get('/admin/facebook/callback', 'App\Http\Controllers\Facebook\FacebookController#handleProviderCallbackAdmin');
I just followed the documentation, changing according to my needs. When I go to page http://local.dev/admin/facebook/login, I get the following error :
Non-static method Laravel\Socialite\Contracts\Factory::driver() cannot be called statically, assuming $this from incompatible context
Indeed, according to the code, driver function must be instanciate.
EDIT : And if I try to instanciate this class, I get the following error :
Cannot instantiate interface Laravel\Socialite\Contracts\Factory
How do you make this module to work?
here's how that works in my case
in services.php file
'facebook' => [
'client_id' => '***************',
'client_secret' => '***************',
'redirect' => ""
],
i left redirect empty cause my site is multilingual (so, it fills in a bit later with sessions). if you use only one language, put there your callback absolute path. for example
"http://example.com:8000/my-callback/";
also check your config/app.php. in providers array
Laravel\Socialite\SocialiteServiceProvider::class,
in aliases array
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
my routes look like this
Route::get('facebook', 'Auth\AuthController#redirectToProvider');
Route::get('callback', 'Auth\AuthController#handleProviderCallback');
here's auth controllers methods. put in top
use Socialite;
//იობანი როტ
public function redirectToProvider(Request $request)
{
return Socialite::with('facebook')->redirect();
}
public function handleProviderCallback(Request $request)
{
//here you hadle input user data
$user = Socialite::with('facebook')->user();
}
my facebook app
giga.com:8000 is my localhost (so its the same localhost:8000)
as you can see in Valid OAuth redirect URI, you should put there your callback. in my case i use three urls cause i have three languages. in your case it should be just
http://your-domain-name.com:8000/callback
if you work on localhost, you should name your domain in config/services.php
mine look like this
'domain' => "your-domain.com",
after everything run these commands
php artisan cache:clear
php artisan view:clear
composer dump-autoload
restart your server, clear your browser cookies. hope it helps

Unable to generate a Cashier PDF in Laravel

I am using Laravel 5 to generate a PDF from a subscription generated from Cashier. The docs say this is as simple as calling:
return $user->downloadInvoice($invoice->id, [
'vendor' => 'Your Company',
'product' => 'Your Product',
]);
Unfortunately I'm getting an odd error:
No hint path defined for [cashier]
The code I am actually using is as follows:
Route::get('billing/invoices/download/{id}', function($id){
$user = Auth::user();
//$invoice = $user->invoices()->find($id);
return $user->downloadInvoice($id, [
'vendor' => 'Certify Me',
//'product' => $invoice->lines->data[0]['plan']->name,
'product' => 'Subscription',
]);
});
The docs make me assume that the PDF is automatically generated. I'd then assume I could override the PDF layout if I chose to.
I just ran into this (L5.1, Cashier 6.0). This seems to be caused by the service provider not being correctly loaded.
Here is how I fixed it:
Check that you have added the correct service provider, at the time of writing that is Laravel\Cashier\CashierServiceProvider to your config/app.php
If it still doesn't work, go run php artisan config:clear to make sure that the service provider is picked up.
Happy invoicing!
I'm going to resurrect this beast.
I had a similar issue because the service provider was not loaded. If you checkout CashierServiceProvider you'll see it adds the necessary 'namespace' for the 'cashier' prefixed views.
public function boot()
{
$this->loadViewsFrom(__DIR__.'/../../views', 'cashier');
$this->publishes([
__DIR__.'/../../views' => base_path('resources/views/vendor/cashier'),
]);
}
Add Laravel\Cashier\CashierServiceProvider to your config/app.php file and inside the providers key.
For anyone who runs across this like we did.

I get Undefined variable when I am using AWS configuration file with the service builder

I'm trying to integrate this PHP framework https://github.com/panique/mini with Amazon S3:s configuration file with the service builder http://docs.aws.amazon.com/aws-sdk-php/guide/latest/credentials.html#using-a-configuration-file-with-the-service-builder
Best would be if I could integrate it with the existing config.php https://github.com/panique/mini/blob/master/application/config/config.php but I don't know how I should do with this line.
$aws = Aws::factory('/path/to/custom/config.php');
Since I already include config.php in another part of the code
This is what I have tried but don't know why it does not work
Created a new file aws-config.php in the folder config and included it in my project. aws-config.php have the following code (with my correct keys).
return array(
// Bootstrap the configuration file with AWS specific features
'includes' => array('_aws'),
'services' => array(
// All AWS clients extend from 'default_settings'. Here we are
// overriding 'default_settings' with our default credentials and
// providing a default region setting.
'default_settings' => array(
'params' => array(
array(
'credentials' => array(
'key' => 'YOUR_AWS_ACCESS_KEY_ID',
'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
)
),
'region' => 'us-west-1'
)
)
)
);
I want to access my credentials in my controller that looks like this: https://github.com/panique/mini/blob/master/application/controller/songs.php
I've implemented it like this from the documentation
<?php
use Aws\S3\S3Client;
use Aws\Common\Aws;
// Create the AWS service builder, providing the path to the config file
$aws = Aws::factory(APP . 'config/aws-config.php');
$client = $aws->get('s3');
class Album extends Controller
{
public function index()
{
foreach ($images as &$image) {
$image->imageThumbnailUrl = $client->getObjectUrl($resizedBucket, 'resized-'.$image->image_name, '+10 minutes');
}
...
...
I get the error message
Notice: Undefined variable: client in Fatal error: Call to a member
function getObjectUrl() on a non-object in
I use $client and getObjectUrl in my loop.
My code works fine if I use "Passing credentials into a client factory method" http://docs.aws.amazon.com/aws-sdk-php/guide/latest/credentials.html#passing-credentials-into-a-client-factory-method in the index method in my controller.
The problem here has nothing to do with the AWS SDK or the panique/mini framework. You cannot declare variables outside of a class definition and expect to be able to use them inside the class definition. That is just how variable scoping works in PHP. You need to pass the s3Client object into the Controller somehow, or instantiate it inside of the controller.
You can literally move these lines into your index method, and it should work.
// Create the AWS service builder, providing the path to the config file
$aws = Aws::factory(APP . 'config/aws-config.php');
$client = $aws->get('s3');

Categories