John Davidson

php - Push notification with pusher beam, unable to send data/payload

0 comments
Message:


I am using following php sdk for generating pusher beam push notification. but there is not information provided in the sdk docs on how to send data/payload along with it. the notification is bean sent correctly but I am unable to send data: please guide me how we can send the data: it is showing hasData as false in beam console, please check:


php sdk i am using : https://github.com/pusher/push-notifications-php


my code look like this:


$pushNotification = new PushNotifications([
'instanceId' => env('BEAM_INSTANCE_ID'),
'secretKey' => env('BEAM_PRIMARY_KEY')
]);
$pushNotification->publishToInterests(
['message-notification'],
[
"apns" => [
"aps" => [
"alert" => "Message Received",
],
],
"fcm" => [
"notification" => [
'title' => 'some title',
'body' => 'some body',
'data' => 'some data', //passing data here but not working
],
],
]
);


My Answer:
> You can send data/payload along with the push notification by including it in the `publish` method of the PushNotifications SDK. Here is an example of how you can send data along with the notification:

php
$pushNotification = new PushNotifications([
'instanceId' => 'YOUR_INSTANCE_ID',
'secretKey' => 'YOUR_SECRET_KEY',
]);

$data = [
'message' => 'Hello, this is a push notification with data',
'custom_data' => 'Custom data here',
];

$publishResponse = $pushNotification->publish(
['interests' => ['hello']],
['fcm' => ['notification' => ['title' => 'Hello', 'body' => 'This is a push notification'], 'data' => $data]]
);

print_r($publishResponse);


In the above code, the `$data` array contains the data that you want to send along with the notification. You can include any custom data that you want to send. The `publish` method takes two parameters: the interests to which the notification should be sent and the notification data. In the notification data, you can include the title, body, and the data array.

Make sure to replace `'YOUR_INSTANCE_ID'` and `'YOUR_SECRET_KEY'` with your actual Pusher Beam instance ID and secret key.

After sending the notification, you should see the data in the Pusher Beam console under the notification details.

Rate this post

3 of 5 based on 2214 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us