I am trying to send web push notification using firebase in angular5 and Codeignitor. After intialize firebase, I am able to get deviceToken to whoom I want to send push notification. When I am trying to send it at backend using curl. It always show me this error
{
"multicast_id":8778443463170192739,
"success":0,
"failure":1,
"canonical_ids":0,
"results":[
{
"error":"MismatchSenderId"
}
]
}
In backend api request, I am using "Server key" which is "AAAA8q7RESs:APA91bFrOrzgvTb0yQTqTF6huozIvgi_5xf1lY8qv5kyoDViuBYnVFr3Qfxg3DZHN_e7-AqWBIvRlQvPJkgZtxKIpCYs_aUUiYHOKuKLLJvzxAENjKmF4mCQJb94tJV8J-vBgnv0FPPY".
But always show same error.
My backend Php code is
public function sendpushnotification() {
$request = json_decode(file_get_contents('php://input'),true);
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => array($request['token']),
'data' => array( "message" => "Hello this is test push notification" ),
);
$headers = array(
'Authorization: key=AAAA8q7RESs:APA91bFrOrzgvTb0yQTqTF6huozIvgi_5xf1lY8qv5kyoDViuBYnVFr3Qfxg3DZHN_e7-AqWBIvRlQvPJkgZtxKIpCYs_aUUiYHOKuKLLJvzxAENjKmF4mCQJb94tJV8J-vBgnv0FPPY',
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
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_POSTFIELDS, json_encode( $fields ) );
// Avoids problem with https certificate
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
// Execute post
$result = curl_exec($ch);
curl_close($ch);
echo json_encode($result);
}
Can anyone helps me. I have spend lot of time for this but not found any solution.
And I have a query. Is deviceToken/deviceID depends upon messageSenderID?
First of all try to send notification from FCM console giving deviceToken.
I have used below code and it's working fine. I wrote this function in helper and used it in multiple controllers.
Second thing: deviceToken/deviceID will be used for the user who will receive message.
function sendFCM($message, $id, $message_info='', $type ='') {
$API_ACCESS_KEY = "YOUR_FCM_SERVER_KEY";
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' => array (
$id
),
'data' => array (
"message" => $message,
'message_info' => $message_info,
),
'priority' => 'high',
'notification' => array(
'title' => $message['title'],
'body' => $message['body'],
),
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . $API_ACCESS_KEY,
'Content-Type: application/json'
);
$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_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
curl_close ( $ch );
}
Related
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
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
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);
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 );
}
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;
}