php 系统通知怎么做
时间 : 2023-03-25 08:21:02声明: : 文章内容来自网络,不保证准确性,请自行甄别信息有效性

PHP系统通知通常是指在Web应用程序中发送通知消息给用户的功能。通知可以包括新消息提醒、新订单提醒等等。本文将介绍如何使用PHP实现系统通知功能。

一、基本思路

实现系统通知功能的基本思路如下:

1.在数据库中维护通知消息的记录,包括通知标题、内容、接收人、发送人、发送时间等信息。

2.用户登录后查询数据库中未读的通知消息,并将其显示在前端页面的通知栏中。

3.当用户阅读通知消息后,将其状态改为已读,并重新查询更新未读通知消息。

4.在后台进行操作时,根据需要向指定用户发送通知消息。

二、具体实现

1.数据库设计

我们需要在数据库中维护通知消息记录。这里我们通过建立两个表来实现:一个是notifications表,用于存储通知消息的主体内容;另一个是notifications_status表,用于存储用户的通知消息状态。

notifications表的设计如下:

CREATE TABLE `notifications` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT '标题',

  `content` text COLLATE utf8_unicode_ci NOT NULL COMMENT '内容',

  `created_by` int(11) NOT NULL COMMENT '发送人id',

  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',

  PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

notifications_status表的设计如下:

CREATE TABLE `notifications_status` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `user_id` int(11) NOT NULL COMMENT '接收人id',

  `notification_id` int(11) NOT NULL COMMENT '通知消息id',

  `is_read` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否已读(0未读,1已读)',

  PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

2.前端显示

用户登录后,可以查询数据库中未读的通知消息,并在前端页面的通知栏中显示出来。这里我们可以使用Bootstrap的Popover组件生成一个弹出式通知消息。

<button type="button" class="btn btn-default" data-toggle="popover" title="通知消息" data-content="<?php echo $notification_content; ?>">

<i class="glyphicon glyphicon-bell"></i>

<span class="badge"><?php echo $notification_count; ?></span>

</button>

3.阅读状态更新

用户点击通知消息后,可以将其状态改为已读,并重新查询未读通知消息。这里我们需要编写一个update语句,将notifications_status表中对应的记录的is_read字段设置为1。

$query = "UPDATE notifications_status SET is_read = 1 WHERE user_id = ".$user_id." AND notification_id = ".$notification_id;

4.发送通知消息

在后台进行操作时,根据需要向指定用户发送通知消息。我们可以写一个函数实现此功能。函数调用的形式如下:

send_notification($title, $content, $user_id);

其中,$title为通知标题,$content为通知内容,$user_id为接收人id。

function send_notification($title, $content, $user_id) {

$created_by = 0; // 发送人id,这里设为0

$query = "INSERT INTO notifications (title, content, created_by) VALUES ('".$title."', '".$content."', '".$created_by."')";

$notification_id = mysqli_insert_id($conn);

$query = "INSERT INTO notifications_status (user_id, notification_id) VALUES ('".$user_id."', '".$notification_id."')";

}

以上就是使用PHP实现系统通知功能的基本思路和具体实现。

在 PHP 中,系统通知可以通过多种方式实现,包括电子邮件、短信、弹窗提示等。下面是其中一种实现方式:

1. 定义系统通知类

可以新建一个名为 Notification 的类来处理系统通知。这个类可以有一些属性和方法,例如:

```php

class Notification {

private $title;

private $message;

private $recipient;

public function __construct($title, $message, $recipient) {

$this->title = $title;

$this->message = $message;

$this->recipient = $recipient;

}

public function sendEmail() {

// 发送邮件的代码

}

public function sendSms() {

// 发送短信的代码

}

public function displayPopup() {

// 显示弹窗提示的代码

}

}

在这个类中,我们定义了标题(title)、消息内容(message)和接收者(recipient)等属性,并且通过构造函数对它们进行初始化。除此之外,还定义了三个方法来分别处理邮件、短信和弹窗提示等不同的通知方式。

2. 实现邮件发送

通过使用 PHP 的邮件发送库(例如 PHPMailer),我们可以很方便地实现邮件的发送功能。下面是一个使用 PHPMailer 发送邮件的例子:

```php

require_once 'path/to/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP();

$mail->SMTPAuth = true;

$mail->Host = 'smtp.gmail.com';

$mail->Username = 'your_email@gmail.com';

$mail->Password = 'your_password';

$mail->SMTPSecure = 'tls';

$mail->Port = 587;

$mail->setFrom('your_email@gmail.com', 'Your Name');

$mail->addAddress($this->recipient);

$mail->Subject = $this->title;

$mail->Body = $this->message;

$mail->send();

我们通过 PHPMailer 类的实例来定义邮件发送的相关属性(例如 SMTP 服务器地址、用户名和密码等),最后调用 send() 方法来发送邮件。

3. 实现短信发送

短信通知功能可以通过使用运营商提供的短信服务 API 来实现。例如,你可以使用阿里云提供的 SMS 服务来发送短信通知。具体的实现过程包括以下几个步骤:

- 注册阿里云账号并开通短信服务;

- 在阿里云控制台中获取 AccessKeyId 和 AccessKeySecret 等 API 密钥;

- 引入阿里云 PHP SDK 并初始化 `DefaultAcsClient` 类;

- 调用 `SendSmsRequest` 接口来发送短信通知。

下面是一个使用阿里云 PHP SDK 发送短信通知的例子:

```php

set_time_limit(0);

require_once 'path/to/aliyun-php-sdk-core/Config.php';

require_once 'path/to/aliyun-php-sdk-core/DefaultAcsClient.php';

require_once 'path/to/aliyun-php-sdk-sms/Sms\Request\V20160927\SendSmsRequest.php';

use DefaultAcsClient;

use DefaultProfile;

use Request\V20160927\SendSmsRequest;

$accessKeyId = '<Your AccessKeyId>';

$accessKeySecret = '<Your AccessKeySecret>';

$signName = '<Your SignName>';

$templateCode = '<Your TemplateCode>';

$profile = DefaultProfile::getProfile('cn-hangzhou', $accessKeyId, $accessKeySecret);

$client = new DefaultAcsClient($profile);

$request = new SendSmsRequest();

$request->setPhoneNumbers($this->recipient);

$request->setSignName($signName);

$request->setTemplateCode($templateCode);

$request->setTemplateParam(json_encode(array(

'title' => $this->title,

'message' => $this->message,

)));

$response = $client->getAcsResponse($request);

在这个例子中,我们引入了阿里云 PHP SDK 中的 `DefaultAcsClient` 和 `SendSmsRequest` 类。我们通过 `$accessKeyId` 和 `$accessKeySecret` 等信息初始化 `DefaultProfile` 和 `DefaultAcsClient` 实例,然后构造 `SendSmsRequest` 请求对象,并设置接收短信的手机号码、短信签名和模板编号等信息。模板参数可以通过 `json_encode` 函数将标题和消息内容打包成 JSON 格式的字符串。最后,我们通过 `getAcsResponse` 方法获取响应结果。

4. 实现弹窗提示

弹窗提示功能可以通过使用 JavaScript 和 HTML 对象实现。我们可以在 HTML 页面中创建一个隐藏的 `div` 元素,并使用 JavaScript 在需要时将其显示出来。具体的实现过程可以参考以下代码:

```php

<script>

function showNotification(title, message) {

var notification = document.getElementById('notification');

var titleElem = notification.querySelector('.title');

var messageElem = notification.querySelector('.message');

titleElem.innerText = title;

messageElem.innerText = message;

notification.style.display = 'block';

setTimeout(function() {

notification.style.display = 'none';

}, 5000);

}

</script>

<div id="notification" style="display:none">

<div class="title"></div>

<div class="message"></div>

</div>

在这个代码中,我们定义了一个名为 `showNotification` 的 JavaScript 函数,它接受标题和消息内容作为参数,并使用 `document.getElementById` 函数来获取隐藏的 `div` 元素。然后,我们将标题和消息内容分别填充到 `titleElem` 和 `messageElem` 元素中,并将 `notification` 元素的 `display` 属性设置为 'block',从而将其显示出来。最后,我们设置一个 5000 毫秒的定时器,使得 `notification` 元素在 5 秒后自动隐藏起来。

这样,我们就实现了通过邮件、短信和弹窗提示等多种方式来发送系统通知的功能。