Please provide the following:
- SDK Version: 37
- Platforms: (Android/iOS)
We are building an app with Expo and We are trying to enable push notifications with our backend service which is written with php (Laravel), We are working with Laravel-Exponent-Push-Notifications
The following code snippet is used to send Notification with in Laravel project:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use NotificationChannels\ExpoPushNotifications\ExpoChannel;
use NotificationChannels\ExpoPushNotifications\ExpoMessage;
class Notification_app extends Notification
{
use Queueable;
public function __construct()
{
//
}
public function via($notifiable)
{
return [ExpoChannel::class];
}
public function toExpoPush($notifiable)
{
return ExpoMessage::create()
->badge(1)
->enableSound()
->title("Congratulations!")
->body("Your {$notifiable->service} account was approved!");
}
public function toArray($notifiable)
{
return [
//
];
}
}
Then calling the function to send notification like so $data->notify(new Notification_app());
On the client side the code is looks like the following:
useEffect(() => {
registerForPushNotificationsAsync();
Notifications.addListener(_handleNotification);
},
[])
const registerForPushNotificationsAsync = async () => {
console.log(Constants.isDevice);
if (Constants.isDevice) {
const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
console.log(finalStatus);
if (finalStatus !== 'granted') {
alert('Failed to get push token for push notification!');
return;
}
let token = await Notifications.getExpoPushTokenAsync();
setExpoPushToken(token);
} else {
alert('Must use physical device for Push Notifications');
}
if (Platform.OS === 'android') {
Notifications.createChannelAndroidAsync('default', {
name: 'default',
sound: true,
priority: 'max',
vibrate: [0, 250, 250, 250],
});
}
};
const _handleNotification = notification => {
console.log(notification);
Vibration.vibrate();
if(notification.origin == 'selected'){
props.navigation.navigate('MyTaskDetails');
}
setNotification();
};
Here we generate Expo push token then I know that we have to send it to backend so that each token will be related to a device which have our app installed, My question here is how to use device token (Expo Push token) in the above Laravel code to send notification to a specific device there is no parameters that refer to any token … any help or snippet of code that might help me here is appreciated.