Download the PHP package computy/yii2-notifications without Composer
On this page you can find all versions of the php package computy/yii2-notifications. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download computy/yii2-notifications
More information about computy/yii2-notifications
Files in computy/yii2-notifications
Package yii2-notifications
Short Description Notifications module for Yii2
License BSD-3-Clause
Informations about the package yii2-notifications
yii2-notifications
This module provides a way to sending notifications across a variety of delivery channels, including mail, screen, SMS (via Nexmo), etc. Notifications may also be stored in a database so they may be displayed in your web interface.
Notifications are short messages that notify users of something that occurred in your application. For example, if you are writing a billing application, you might send an "Invoice Paid" notification to your users via the email and SMS channels.
This is a fork of webzop/yii2-notifications. Relevant information for migrating from webzop/yii2-notifications to computy/yii2-notifications is listed under migration.
Requirements
- PHP 8.1+
- gmp
- mbstring
- curl
- openssl
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the require section of your composer.json
file.
Usage
Notifications is often used as an application module and configured in the application configuration like the following:
To enable Web Push Notifications browsers is needed to verify your identity. A standard called VAPID can authenticate the application for all browsers. You'll need to create and provide a public and private key for your server. These keys must be safely stored and should not change.
If you want to disable the Web Push Notifications simply set the flag 'notifications.web.enable' to false.
In order to generate the uncompressed public and secret key, encoded in Base64, enter the following in your Linux bash:
Or you can use this method provided in the module:
Create A Notification
Each notification is represented by a single class (typically stored in the app/notifications directory).
Send A Notification
Once the notification is created, you can send it as following:
Specifying Delivery Channels
Every notification class has a shouldSend($channel) method that determines on which type of keys and channels the notification will be delivered. In this example, the notification will be delivered in all channels except "screen" or with key "new_account":
It is possible to limit the amount of same key/user notifications sent to a user through the renotification_time
property of the notification.
A new notification will be sent only if there aren't others with same user/key sent in the period specified in the property.
renotification_time
has to be a string accepted by DateInterval::__constructor().
Specifying The Send For Specific Channel
Every channel have a send method that receive a notification instance and define a way of that channel will send the notification. But you can override the send method by define toMail ("to" + [Channel ID]) in notification class. This example show how to do that:
Custom Channels
This module have a some pre-built channels, but you may want to write your own channels to deliver notifications. To do that you need define a class that contains a send method:
You also should configure the channel in you application config:
Screen Channel
This channel is used to show small notifications as above image preview. The notifications will stored in database, so before using this channel, you have to run its migrations scripts:
So you can call the Notifications widget in your app layout to show generated notifications:
Web Push Notification Channel
This channel is used to send web push notification to subscriber. Each notification subscription will be stored in the database, so before using this channel, you have to run its migrations scripts:
So you can call the Notifications widget in your app layout to show generated notifications:
You can customize the HTML template for the widget as follow. Setting template to false will hide all widget HTML and the browser will prompts the user to allow notifications. If you customize the HTML template remember to include a button with id 'js-web-push-subscribe-button':
Remember to place the service-worker.js file in the web root in order to serve the service worker when the WebNotifications widget is initialized.
Migration
The following breaking changes from webzop/yii2-notifications to computy/yii2-notifications need to be considered when migrating:
- This version only supports PHP 8.1 and higher
- Accordingly, the dependency to minishlink/web-push has been upgraded to version 9.0.0.
- Browsers that cannot handle ES6 syntax cannot display the notifications widget anymore.
- This version uses the computy/notifications namespace rather than webzop/notifications