Laravel Push notification to Android and iOS

Laravel Push notification to Android and iOS

Sharing is caring!

Laravel Push notification to Android and iOS

Hello Friends, Today we learn, How to send a push notification to Android and iOS mobile using Laravel Framework. A push notification is a message which appears in the mobile status bar.

So let’s get started.

For sending push notification we use Laravel plugin named Edujugon Push Notification. One more thing is, usage of this plugin is changing as per your Laravel Version.

Laravel Version 5.0-5.1

 

composer require edujugon/push-notification:dev-laravel-5

Run above in console.

Laravel Version 5.2 and Higher

composer require edujugon/push-notification

Run Above command in the console.

Laravel 5.*

If you use Laravel version 5.5 or higher then you no need to register package alias because it has package auto-discovery feature in Laravel 5.5 and higher.

Only if Laravel Version < 5.5

'providers' => array(
    Edujugon\PushNotification\Providers\PushNotificationServiceProvider::class
)
'aliases' => array(
    'PushNotification' => Edujugon\PushNotification\Facades\PushNotification::class,
)

Add above line in your Laravel project to register push notification service for your project.

After successful installation of the plugin, you have to publish your package, run following command in your console.

php artisan vendor:publish --provider="Edujugon\PushNotification\Providers\PushNotificationServiceProvider" --tag="config"

After publishing your plugin now you need to set configuration in config/pushnotification.php

The default parameter of  GCM(Google Cloud Messaging) and FCM(Firebase cloud messaging) is

  • priority => normal
  • dry_run => false
  • apiKey => Your ApiKey

If you are using this for iOS than your configuration is

  • certificate => __DIR__ . '/iosCertificates/yourCertificate.pem'
  • passPhrase => 'MyPassPhrase'
  • passFile => __DIR__ . '/iosCertificates/yourKey.pem' //Optional
  • dry_run => false

Usage of Plugin

$push = new PushNotification;

This is a default service which is GCM of FCM

You are using APN than you can change it as follow

$push = new PushNotification('apn');

And For FCM

$push = new PushNotification('fcm');

To send a notification you have to set your API key (GCM and FCM) or Certificate.pem path (APN)

 

Usage Example

For GCM

$push->setMessage([
        'notification' => [
                'title'=>'This is the title',
                'body'=>'This is the message',
                'sound' => 'default'
                ],
        'data' => [
                'extraPayLoad1' => 'value1',
                'extraPayLoad2' => 'value2'
                ]
        ])
        ->setApiKey('Server-API-Key')
        ->setDevicesToken('deviceToken')
        ->send();

 

For APN

$push->setMessage([
            'aps' => [
                'alert' => [
                    'title' => 'This is the title',
                    'body' => 'This is the body'
                ],
                'sound' => 'default',
                'badge' => 1

            ],
            'extraPayLoad' => [
                'custom' => 'My custom data',
            ]
        ])
    ->setDevicesToken('deviceToken')
    ->send();

For send multiple devices you can set device tokens as follows.

 

From above you can send Notification.

But how can you ensure that your notification is sent or there is an error?

You can find this,

PushNotification::setService('fcm') // change as per your requirement FCM / GCM / APN
                        ->setMessage([
                             'notification' => [
                                     'title'=>'This is the title',
                                     'body'=>'This is the message',
                                     'sound' => 'default'
                                     ],
                             'data' => [
                                     'extraPayLoad1' => 'value1',
                                     'extraPayLoad2' => 'value2'
                                     ]
                             ])
                        ->setApiKey('Server-API-Key') // only if you are using GCM/FCM
                        ->setDevicesToken(['deviceToken1','deviceToken2','deviceToken3'...])
                        ->send()
                        ->getFeedback();

This is the Complete code of Notification

 
public function send_method_in_apn_service(){
    $push = new PushNotification('apn');
    $message = [
        'aps' => [
            'alert' => [
                'title' => '1 Notification test',
                'body' => 'Just for testing purposes'
            ],
            'sound' => 'default'
        ]
    ];
    $push->setMessage($message)
        ->setDevicesToken([
            '507e3adaf433ae3e6234f35c82f8a43a0d84218bff08f16ea7be0869f066c0312',   
            'ac566b885e91ee74a8d12482ae4e1dfdda1e26881105dec262fcbe0e082a358',,   
            '507e3adaf433ae3e6234f35c82f8a43add84218bff08f16ea7be0869f066c0312',  
        ]);
    $push = $push->send();
 $push->getFeedback()); 
}

 

 

So this is a complete tutorial for Sending Push notification using Laravel.

If you are getting any error comment below.

Laravel Push notification to Android and iOS

28 thoughts on “Laravel Push notification to Android and iOS

  1. Hi to every , as I am actually eager of reading this web
    site’s post to be updated on a regular basis. It consists
    of good material.

  2. It was great discovering your website recently. I arrived here nowadays hunting new things.
    I was not frustrated. Your suggestions after new approaches on this thing have
    been beneficial plus a fantastic help to me personally. We appreciate you
    having time to write out these items and then for sharing your ideas.

  3. I see your point, and I completely enjoy your article.
    For what its really worth I will tell all my friends about
    it, quite creative.

  4. Thanks for the wonderful blog. It was very helpful for me.
    Keep sharing such tips in the future as well. This was actually what I was looking for, and I’m
    glad I got here! Many thanks for sharing the such information here.

  5. I’ve been reading lots of your articles and
    i must say very good stuff. I will certainly bookmark your site.

  6. Thanks, I’ve just been looking for information about this topic for a while and yours is the greatest I’ve discovered until now.

  7. I’m gone to inform my little brother, that he should also pay a visit this website on regular basis to obtain updated from most recent gossip.

  8. I haven’t checked in here for a little while because I was
    thinking it was getting boring, yet the last few posts are really good quality so
    I guess I’ll add you back to my day-to-day bloglist.

    You deserve it my buddy.

  9. Beneficial information as well as excellent design you have here!
    I desire to thank you for sharing your ideas and
    putting the time into the stuff you publish!
    Fantastic work!

  10. I do believe all the ideas you have presented for your post.

    They’re really convincing and can surely work.

    Still, the posts are really quick for newbies. Thank you for the post.

  11. Hello.. Thank you for such a great article.It is very helpful. But I am facing one problem. I want to send the payload data to the device in JSON format but the $push->setMessage($message) only accepts array value and I want to send the JSON to the device. Please help me with this. Thanks in advance.

  12. Code: public function send_method_in_apn_service(){
    $push = new PushNotification(‘apn’);
    $deviceToken = ‘19282c692b4da1c1ba1994d624bfe40ae205a2b0f9b1c64da6961b4681bdbaac’;

    $push->setMessage([
    ‘apn’ => [
    ‘alert’ => [
    ‘title’ => ‘This is the title’,
    ‘body’ => ‘This is the body’
    ],
    ‘sound’ => ‘default’,
    ‘badge’ => 1

    ],
    ‘extraPayLoad’ => [
    ‘custom’ => ‘My custom data’,
    ]
    ])
    ->setDevicesToken([
    ‘19282c692b4da1c1ba1994d624bfe40ae205a2b0f9b1c64da6961b4681bdbaac’,
    ‘ac566b885e91ee74a8d12482ae4e1dfdda1e26881105dec262fcbe0e082a358’,
    ‘507e3adaf433ae3e6234f35c82f8a43add84218bff08f16ea7be0869f066c0312’,
    ])
    ->send()
    ->getFeedback();

    dd($push->send());

    }

    Result:
    PushNotification {#468
    #service: Apn {#446
    -sandboxUrl: “ssl://gateway.sandbox.push.apple.com:2195”
    -productionUrl: “ssl://gateway.push.apple.com:2195”
    -feedbackSandboxUrl: “ssl://feedback.sandbox.push.apple.com:2196”
    -feedbackProductionUrl: “ssl://feedback.push.apple.com:2196”
    -feedbackUrl: “ssl://feedback.sandbox.push.apple.com:2196”
    -maxAttempts: 3
    -attempts: 0
    #url: “ssl://gateway.sandbox.push.apple.com:2195”
    #config: array:4 [
    “certificate” => “/var/www/html/saloon-final-transaction/config/iosCertificates/OnDemandVendor_PemFile.pem”
    “passPhrase” => “12345678”
    “passFile” => “/var/www/html/saloon-final-transaction/config/iosCertificates/OnDemandVendor_PemFile.pem”
    “dry_run” => true
    ]
    #feedback: {#455
    +”success”: 3
    +”failure”: 0
    +”tokenFailList”: []
    }
    }
    #servicesList: array:3 [
    “gcm” => “Edujugon\PushNotification\Gcm”
    “apn” => “Edujugon\PushNotification\Apn”
    “fcm” => “Edujugon\PushNotification\Fcm”
    ]
    -defaultServiceName: “gcm”
    #deviceTokens: array:3 [
    0 => “19282c692b4da1c1ba1994d624bfe40ae205a2b0f9b1c64da6961b4681bdbaac”
    1 => “ac566b885e91ee74a8d12482ae4e1dfdda1e26881105dec262fcbe0e082a358”
    2 => “507e3adaf433ae3e6234f35c82f8a43add84218bff08f16ea7be0869f066c0312”
    ]
    #message: array:2 [
    “apn” => array:3 [
    “alert” => array:2 [
    “title” => “This is the title”
    “body” => “This is the body”
    ]
    “sound” => “default”
    “badge” => 1
    ]
    “extraPayLoad” => array:1 [
    “custom” => “My custom data”
    ]
    ]
    }
    This is my array in send notification but i didn’t get notification in ios

    1. Hey Chirag,
      This code is working because it’s showing a message
      +” success”: 3 // notification was sent to all three device.
      +” failure”: 0
      +”tokenFailList”: []

      I suggest checking your device token once.

      And also check your .pem file path is perfect or not.

    1. yes but then you have to set .pem file path every time while you run this service in the controller.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top