How to send notification with image to IOS and android in PHP - laravel

I have a Laravel app in which I use some code to send FCM to send notification to IOS and Android, but now I need to include and image in the notification.
the code I am using now is as below
$fcmUrl = 'https://fcm.googleapis.com/fcm/send';
$notification = [
'body' => 'Message',
'title'=>'title',
'sound' => true,
'priority' => "high",
'vibration'=>true,
'sound'=> "Enabled",
'badge'=>4,
'id'=>2
];
$extraNotificationData = ['one'=>1,'two'=>2];
$fcmNotification = [
//'registration_ids' => json_encode($token,JSON_FORCE_OBJECT), //multple token array
'to' => $this->token, //single token
'data' => $extraNotificationData,
'notification' => $notification
];
$headers = [
'Authorization: key=Server_key',
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $fcmUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
$result = curl_exec($ch);
curl_close($ch);

Related

How to add Action Url in Firebase push notification

How to add Action URL in Firebase push notification with Laravel
$firebaseToken = $deviceToken;
$SERVER_API_KEY = env('FCM_SERVER_KEY');
$data = [
"to" => $firebaseToken,
"notification" => [
"title" => $notificationTitle,
"body" => $notificationMessage,
"image" => $companyLogo,
"url" => $actionURL,
]
];
$dataString = json_encode($data);
$headers = [
'Authorization: key=' . $SERVER_API_KEY,
'Content-Type: application/json',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
$response = curl_exec($ch);
The code is working fine. I am getting push notifications. But my issue is When clicking on push notification it redirects to the Home page of App, not to the Url given

Push notification from Laravel using google firebase

I am trying to send push notification using google firebase , but notification is not coming to phone
public function pushNotificationToUsersTest(){
$apikey = env('FCM_SERVER_KEY');
$deviceToken = array("deviceTocken_1,deviceTocken_2");
$url = 'https://fcm.googleapis.com/fcm/send';
$message = "Message from server";
foreach ($deviceToken as $key => $Token) {
$preview_message = "ACTIONABLE";
$sound = "default";
$fields = array(
'to' => $Token,
'data' => array(
'message' => $message,
'notification_type' => "message_type",
'message_content' => "message_content",
'chat_type' => "chat_type",
'badge' => 10,
'category' => $preview_message,
'sound' => $sound,
)
);
//header with content_type api key
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$apikey
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
}
print_r($result);
}
When I print Result I am getting like this
{"multicast_id":1610930223174111380,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1657873770379569%73be5013f9fd7ecd"}]}1
When I checked respose code
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
It gives output 400.
Don't know what the issue

Laravel send notification to android using firebase

i am sending notification to android using backend as laravel through firebase but i get error as "append .json to your request URI to use the REST API." Using Postman i am runing this api
This is function which is used to get notification in json in my controller
public function sendNotification(Request $req)
{
$fcmUrl = 'https://playout-52d1f.firebaseio.com';
$token=$req->input('token');
$title=$req->input('title');
$notification = [
'title' => $title,
'sound' => true,
];
$extraNotificationData = ["message" => $notification,"moredata" =>'dd'];
$fcmNotification = [
//'registration_ids' => $tokenList, //multple token array
'to' => $token, //single token
'notification' => $notification,
'data' => $extraNotificationData
];
$headers = [
'Authorization: key=AIzaSyDF4_rqij_Bt4zC3GNEVbNFvwX1HHxy5Mo',
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$fcmUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
$result = curl_exec($ch);
curl_close($ch);
dd($result );
}

Mailchimp API "Marketing permission ID '' does not exist." while passing valid id

I use the Mailchimp API to first get the marketing_permission_id for my list. The response looks like this
[marketing_permission_id] => f878932739
This value is then used in a second api call to update the settings for a specific user. However, I get the error:
string(227)
"{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Bad
Request","status":400,"detail":"Marketing permission ID '' does not
exist.", [...]
I double checked the data that is being send in the second call and it has the correct marketing_permission_id in there:
{"marketing_permissions":{"marketing_permission_id":"f878932739","enabled":true}}
I followed the format from https://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#%20.
I don't understand what's going wrong. Hopefully someone here does.. :)
Just leaving this for future reference - there're not many hints on google for mailchimp API with marketing permissions...
This is the basic function i use for adding a contact to a list.
function syncMailchimp($userdata,
$apiKey, $listId)
{
$dataCenter = substr($apiKey,
strpos($apiKey, '-') +
1);
$url = 'https://'.$dataCenter.
'.api.mailchimp.com/3.0/lists/'
.$listId.
'/members/';
// This is the interesting part //
$json = json_encode([
'email_address' => $userdata[
'email'],
'status' => $userdata['status'], // "subscribed","unsubscribed","cleaned","pending"
'merge_fields' => [
'FNAME' => $userdata[
'firstname'
],
'LNAME' => $userdata['lastname']
],
'tags' => [
'added'
],
// How to build the Array -->
'marketing_permissions' =>
array(
0 =>
array(
'marketing_permission_id' =>
'1e5142bbce',
'enabled' =>
true,
),
),
]);
$ch = curl_init($url);
curl_setopt($ch,
CURLOPT_HTTPAUTH,
CURLAUTH_BASIC);
$headers = array(
'Content-Type:application/json',
'Authorization: apikey '
.$apiKey
);
curl_setopt($ch,
CURLOPT_HTTPHEADER,
$headers);
curl_setopt($ch,
CURLOPT_RETURNTRANSFER,
true);
curl_setopt($ch,
CURLOPT_TIMEOUT, 10);
curl_setopt($ch,
CURLOPT_CUSTOMREQUEST,
'POST');
curl_setopt($ch,
CURLOPT_SSL_VERIFYPEER,
false);
curl_setopt($ch,
CURLOPT_POSTFIELDS,
$json);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch,
CURLINFO_HTTP_CODE);
curl_close($ch);
return $result;
}

Add Member in List mailchimp api not working [list memeber] cURL: The API key provided is linked to a different datacenter (status:403)

Mailchimp list add member using API Not working
Request:
$apikey = '#####################-us15';
$data = array(
'email_address' => 'test+mailchimp#gmail.com',
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => 'taiabur rahman'
)
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://us2.api.mailchimp.com/3.0/lists/6de91c2e1c/members/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("content-type: application/json"));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERPWD, "apikey:" . $apikey);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$response_string = curl_exec($ch);
$curl_info = curl_getinfo($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
echo '<pre>';
print_r($response_string);
die('Mailchimp executed');
Response:
{ "type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
"title":"Wrong Datacenter",
"status":403,
"detail":"The API key provided is linked to a different datacenter",
"instance":""
}
Hi Change the url to https://us15.api.mailchimp.com/3.0/ on mailchimp documentation
if the last part of your MailChimp API key is us6, all API endpoints
for your account are available at https://us6.api.mailchimp.com/3.0/.
you can read the full documentation here

Resources