Giter Site home page Giter Site logo

laravel-expo-push-notifications's Introduction

Laravel Expo Push Notifications

Latest Version on Packagist Total Downloads

An Expo Push Notifications driver for Laravel Notifications.

Automatically expires PushTokens if they fail due to DeviceNotRegistered error, and won't use them again.

Stores data about Push Notification delivery status.

Installation

Via Composer

$ composer require relative/laravel-expo-push-notifications

Run migrations

$ php artisan migrate

Optional: Publish migrations & configuration

$ php artisan vendor:publish --provider="Relative\LaravelExpoPushNotifications\ExpoPushNotificationsServiceProvider"

If you use UUIDs for your model id fields, publish the migrations and follow the instructions in the file to switch to string id columns.

Usage

Setup your notifiable users

To get started, add the HasPushTokens trait to your notifiable class(es), e.g. your App\User model

<?php

use Relative\LaravelExpoPushNotifications\Traits\HasPushTokens;

class User {
    use Notifiable, HasPushTokens;
    
    //
}

Register Push Tokens to your users

Your Expo app will be able to generate a Push Token and POST it to a controller method in your Laravel application, which can then register the token to that user, for example:

<?php

class PushNotificationController extends \Illuminate\Routing\Controller {

    public function register(Request $request)
    {
        $token = $request->input('token');
        $request->user()->pushTokens()->firstOrCreate(
            ['token' => $token],
            ['token' => $token],
        );
        return response()->status(200);
    }

}

Notify a user about something

Add ExpoPushNotifications to your Notifiable object

<?php

use Illuminate\Bus\Queueable;
use Relative\LaravelExpoPushNotifications\ExpoPushNotifications;
use Relative\LaravelExpoPushNotifications\PushNotification;

class NewOrder extends \Illuminate\Notifications\Notification {

    use Queueable;
    
    public $order;

    /**
     * Create a new notification instance.
     *
     * @param $order
     */
    public function __construct($order)
    {
        $this->order = $order;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [ExpoPushNotifications::class];
    }

    public function toExpoPushNotification($notifiable)
    {
        return (new PushNotification)
            ->title('New order received')
            ->body("Order #{$this->order->id} is ready for processing");
    }

}

The constructor of the PushNotification class accepts an array of parameters matching the schema defined here: https://docs.expo.io/push-notifications/sending-notifications/#message-request-format

Alternatively you can use the expressive API, in Laravel style as shown above.

The PushNotification class has constants for the priority and sound parameters:

PushNotification::PRIORITY_HIGH;
PushNotification::PRIORITY_NORMAL;
PushNotification::PRIORITY_DEFAULT;

PushNotification::SOUND_DEFAULT;
PushNotification::SOUND_NONE;

Change log

Please see the changelog for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email author email instead of using the issue tracker.

Credits

License

MIT License. Please see the license file for more information.

laravel-expo-push-notifications's People

Contributors

livijn avatar nickcousins avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

laravel-expo-push-notifications's Issues

Add a 'read' status to messages

To complete the package we could add a timestamp for 'read_at' to the notifications, which could then be called from a controller method when anyone wants to implement a notification centre in their app. Pulling notifications from the database is currently very easy but if we had a "read" field, we could then manage the badge on iOS too if we wanted.

Need to add configurable defaults

Ideally, the package should allow all fields, except title, subtitle, body and data to have configurable defaults in case users want a standard TTL, or sound setting for example for all notifications sent from their app.

Undefined array key "6b158d5c-78bc-407b-a837-4f5a6a471571"

I am getting this error referencing this line:

$pushNotification->errors = $receipts[$pushNotification->ticket]['details']['error'];

This is strange since ticket 6b158d5c-78bc-407b-a837-4f5a6a471571 doesn't have an error. This is the receipt:

{
  "data": {
    "5078bd5a-de49-4ca2-91a3-ce15573febaa": {
      "status": "ok"
    },
    "6b158d5c-78bc-407b-a837-4f5a6a471571": {
      "status": "ok"
    },
    "91fd968a-e585-46c9-84cd-88fbf220ac9f": {
      "status": "error",
      "message": "Unable to retrieve the FCM server key for the recipient's app. Make sure you have provided a server key as directed by the Expo FCM documentation.",
      "details": {
        "fault": "developer",
        "error": "InvalidCredentials",
        "sentAt": 1650455334
      },
      "__debug": []
    }
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.