Laravel Expo 推送通知 - Android 未收到通知
Laravel Expo Push Notification - Not receiving Notification on Android
我是 Laravel 的新手,找不到库 https://github.com/Alymosul/laravel-exponent-push-notifications 的任何实际示例。
我想创建一个简单的欢迎通知。
我的通知是这样的:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use NotificationChannels\ExpoPushNotifications\ExpoChannel;
use NotificationChannels\ExpoPushNotifications\ExpoMessage;
class WelcomeNotification extends Notification
{
use Queueable;
public function __construct(){
}
public function via($notifiable)
{
return [ExpoChannel::class];
}
public function toExpoPush($notifiable)
{
return ExpoMessage::create()
->badge(1)
->title("Hello World!")
->enableSound()
->body("Hello World!");
}
public function toArray($notifiable)
{
return [
];
}
}
我已经在订阅路由的帮助下订阅了一个用户(成功创建了一个数据库条目)。
现在我要向用户发送通知:
public function sendNotification(Request $request)
{
$getUserByEmail = User::where('email', 'user@email.com')->first();
$getUserByEmail->notify(new WelcomeNotification());
}
我没有收到通知。使用 expo 通知工具时,它按预期工作。
你能解释一下我做错了什么吗?
我明白了。问题是,库 laravel-exponent-push-notifications 将所有没有消息通道的通知发送到通道 'Default'.
所以如果我在 设备.
上创建 消息通道 'Default' 就可以了
另外还有两个选择:
选项 1:
在设备上创建消息通道。
import { Notifications } from 'expo';
if (Platform.OS === 'android') {
await Notifications.createChannelAndroidAsync('chat-messages', {
name: 'Chat messages',
sound: true,
});
}
- 发送通知
$getUserByEmail = User::where('email', 'user@email.com')->first();
$getUserByEmail->notify(new WelcomeNotification());
- 通知应包含在设备上注册的消息通道:
public function toExpoPush($notifiable){
return ExpoMessage::create()
->badge(1)
->title("Hello World!")
->enableSound()
->body("Hello World!")
->setChannelId("chat-messages");
}
选项 2:
将文件 NotificationChannels\ExpoPushNotifications\
ExpoMessage.php
中的 toArray()-method 更改为如下内容:
public function toArray()
{
$returnArray = [
'title' => $this->title,
'body' => $this->body,
'sound' => $this->sound,
'badge' => $this->badge,
'ttl' => $this->ttl,
'channelId' => $this->channelId,
'data' => $this->jsonData,
];
if (strtolower($this->channelId) == 'default' || $this->channelId == '') {
unset($returnArray['channelId']);
}
return $returnArray;
}
在没有频道的情况下向 expo 应用程序发送通知时,expo 自动创建频道,您将收到通知。
更新
另外不要忘记在 Laravel 中注册用户以便能够接收推送通知。您可以使用自定义方法或提供的方法执行此操作。
路由可以在以下文件中找到(也可以更改):
vendor\alymosul\laravel-exponent-push-notifications\src\Http\routes.php
.
我是 Laravel 的新手,找不到库 https://github.com/Alymosul/laravel-exponent-push-notifications 的任何实际示例。 我想创建一个简单的欢迎通知。
我的通知是这样的:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use NotificationChannels\ExpoPushNotifications\ExpoChannel;
use NotificationChannels\ExpoPushNotifications\ExpoMessage;
class WelcomeNotification extends Notification
{
use Queueable;
public function __construct(){
}
public function via($notifiable)
{
return [ExpoChannel::class];
}
public function toExpoPush($notifiable)
{
return ExpoMessage::create()
->badge(1)
->title("Hello World!")
->enableSound()
->body("Hello World!");
}
public function toArray($notifiable)
{
return [
];
}
}
我已经在订阅路由的帮助下订阅了一个用户(成功创建了一个数据库条目)。
现在我要向用户发送通知:
public function sendNotification(Request $request)
{
$getUserByEmail = User::where('email', 'user@email.com')->first();
$getUserByEmail->notify(new WelcomeNotification());
}
我没有收到通知。使用 expo 通知工具时,它按预期工作。
你能解释一下我做错了什么吗?
我明白了。问题是,库 laravel-exponent-push-notifications 将所有没有消息通道的通知发送到通道 'Default'.
所以如果我在 设备.
上创建 消息通道 'Default' 就可以了另外还有两个选择:
选项 1: 在设备上创建消息通道。
import { Notifications } from 'expo';
if (Platform.OS === 'android') {
await Notifications.createChannelAndroidAsync('chat-messages', {
name: 'Chat messages',
sound: true,
});
}
- 发送通知
$getUserByEmail = User::where('email', 'user@email.com')->first();
$getUserByEmail->notify(new WelcomeNotification());
- 通知应包含在设备上注册的消息通道:
public function toExpoPush($notifiable){
return ExpoMessage::create()
->badge(1)
->title("Hello World!")
->enableSound()
->body("Hello World!")
->setChannelId("chat-messages");
}
选项 2:
将文件 NotificationChannels\ExpoPushNotifications\
ExpoMessage.php
中的 toArray()-method 更改为如下内容:
public function toArray()
{
$returnArray = [
'title' => $this->title,
'body' => $this->body,
'sound' => $this->sound,
'badge' => $this->badge,
'ttl' => $this->ttl,
'channelId' => $this->channelId,
'data' => $this->jsonData,
];
if (strtolower($this->channelId) == 'default' || $this->channelId == '') {
unset($returnArray['channelId']);
}
return $returnArray;
}
在没有频道的情况下向 expo 应用程序发送通知时,expo 自动创建频道,您将收到通知。
更新
另外不要忘记在 Laravel 中注册用户以便能够接收推送通知。您可以使用自定义方法或提供的方法执行此操作。
路由可以在以下文件中找到(也可以更改):
vendor\alymosul\laravel-exponent-push-notifications\src\Http\routes.php
.