Sending Firebase push notifications from Laravel - ios

I am writing an iOS application, using laravel for API and google firebase for push notifications. When I send a push notification using firebase cloud messaging, it comes to my device. When I send push notifications using laravel, it does not affect. Here is my script for sending push notifications from laravel:
function sendNotification(Request $request)
{
$friendToken = [];
$usernames = $request->all()['friend_usernames'];
$dialog_id = $request->all()['dialog_id'];
foreach ($usernames as $username) {
$friendToken[] = DB::table('users')->where('user_name', $username)
->get()->pluck('device_token')[0];
}
$url = 'https://fcm.googleapis.com/fcm/send';
foreach ($friendToken as $tok) {
$fields = array(
'to' => $tok,
'data' => $message = array(
"message" => $request->all()['message'],
"dialog_id" => $dialog_id
)
);
$headers = array(
'Authorization: key=*mykey*',
'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_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_exec($ch);
curl_close($ch);
}
$res = ['error' => null, 'result' => "friends invited"];
return $res;
}
It returns a successful result, but notification not sent to iOS devices.
PS: It works successfully on Android devices.

After some research I have found a solution to my question. In order to work for iOS platform, we need to add notification in fields:
$notification = array('title' =>"" , 'text' => $request->all()['message']);
$fields = array(
'to' => $tok,
'data' => $message = array(
"message" => $request->all()['message'],
"dialog_id" => $dialog_id
),
'notification' => $notification
);

Related

Hubspot Webhooks: Redirect Auth & BAD_GRANT_TYPE Message

I'm trying to add a custom app in Hubspot. I'm using PHP. I have:
A Hubspot app
A working webhook URL
A redirect URL with the following code
$url = 'https://api.hubapi.com/oauth/v1/token';
$code = $_GET['code']; // Getting code parameter from redirect URL
//echo $code;
$fields = array();
$fields['grant_type'] = 'authorization_code'; // Have also tried 'refresh_token' - same result
$fields['client_id'] = '{CLIENT_ID}';
$fields['client_secret'] = '{CLIENT_SECRET}';
$fields['redirect_uri'] = '{REDIRECT_URI}';
$fields['code'] = trim($code);
//$fields['refresh_token'] = trim($code); // Have also tried 'refresh_token' - same result
$request_type = 'POST';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', $headers));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, POST);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
if($fields){
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close($ch);
echo var_dump ($return);
My understanding is that Hubspot will use the 'code' parameter passed to the on the "authorization" URL to validate the authorization. However, I keep getting this error:
string(171) "{"status":"BAD_GRANT_TYPE","message":"missing or unknown grant type","correlationId":"f0508752-24bc-40e4-b0bc-51e358459653","requestId":"96044613d4e4a5aab7356fab04001e5c"}"
I had the same error response and it seemed that this is an encoding issue.
I debugged with Postman and found out that the following code works:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.hubapi.com/oauth/v1/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "grant_type=authorization_code&code=string&redirect_uri=string&client_id=string&client_secret=string",
CURLOPT_HTTPHEADER => array(
"content-type: application/x-www-form-urlencoded",
"Cookie: __cfduid=someidfrompostman"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

message attachment sent by office365 rest api is not shown in inbox

I am using office 365 rest api and sending attachment, when I am sending attachment to any email address other than office 365 mail like(gmail.com, yahoo.com etc) then attachment seen in inbox, but in case of office 365 mail it showing only content.
Thanks,
Shankar
I am able to send attachment to both office 365 and yahoo, gmail etc by using rest api of office 365.
Attachment array need to created as follows
<?php
$k=0;
$attchmentArr=array();
foreach($requestjson['name'] as $file){
$attchmentArr[]=array(
"#odata.type"=> "#Microsoft.OutlookServices.FileAttachment",
"Name"=> $file,
"ContentBytes"=> $requestjson['content_bytes'][$k],
);
$k++;
}
?>
You need to create an array like following.
<?php
$post_data=array(
"Message"=>array(
"Subject"=>$requestjson['subject'],
"Body"=>array(
'ContentType'=>'HTML',
'Content'=> $requestjson['message'],
),
"From"=>array(
"EmailAddress"=>array(
'Name'=>$fromName[0],
'Address'=>CARE_ACOCUNT
)
),
"ToRecipients"=>array(
array(
"EmailAddress"=>array(
'Name'=>$toName[0],
"Address"=>$requestjson['to_address']
)
)
),
"Attachments"=>$attchmentArr,
),
"SaveToSentItems"=> "true"
);
?>
then use curl to send mail
<?php
$url="https://outlook.office365.com/api/beta/me/sendmail";
$post_data = json_encode($post_data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_USERPWD, CARE_ACOCUNT . ':' . CARE_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $post_header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response = json_decode($response, true);
$status_arr=array(200,201,202,204);
curl_close($ch);
if(in_array($status,$status_arr)){
return $response;
} else {
return 'error';
}

iOS empty Firebase Push notification doesn't show up

I'm having a weird problem with Firebase notification on iOS. It works perfectly but the only problem is if I keep the 'body' parameter on the server empty it does play notification sound on the app but doesn't show anything. Same thing displays notification on Android. Is it some kind of Apple security that I'm running into? if I only add an empty space " " in 'body' it would show up but nothing without it. If any body could suggest me anything please.
my server side code
$msg = array
(
'title' => '',
'body' => ' ',
'click_action' => '.InstagramLoader',
'icon' => 'myicon',/*Default Icon*/
'sound' => 'mySound'/*Default sound*/
);
$data = array(
'text' => $title,
'img_url' => $img_url
);
$fields = array
(
'to' => $registrationIds,
'notification' => $msg,
'data' => $data
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
#Send Reponse To FireBase Server
$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_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
Yes that's an iOS thing, why show an empty push notification right? You could use isNaN() in your server side code and check for an empty body and set it to a default value.

iOS Swift - Is it possible to send notifications using FCM directly from PHP?

I'm sending push notifications using FCM to Android with MySSQL and PHP. This is my code:
<?php
function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = YOUR_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_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
$conn = mysqli_connect("localhost","root","","fcm");
$sql = " Select Token From users";
$result = mysqli_query($conn,$sql);
$tokens = array();
if(mysqli_num_rows($result) > 0 ){
while ($row = mysqli_fetch_assoc($result)) {
$tokens[] = $row["Token"];
}
}
mysqli_close($conn);
$message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE");
$message_status = send_notification($tokens, $message);
echo $message_status;
?>
But I tried sending notifications on iOS(Swift) devices and it does not work. I get the token (FIRInstanceID.instanceID().token()) and try to send a notification from send.php (above code) and not worked.
I send individual/group/topic notification from Firebase Console and worked perfectly. Why doesn't it work from file.php?
help?!
To send iOS through FCM, you need to define notification key in your payload. It is explained in docs please take a look at there. https://firebase.google.com/docs/cloud-messaging/ios/send-multiple
Here is an example request
{
"condition": "'all' in topics",
"priority" : "high",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message Title",
}
}
In the example above I'm sending to the topic named all. You should create topic and subscribe your users to related topics.
You need to make necessary changes in your $fields variable.

How I can get account id and clientFolderId in icontact API

My question is specific for iContact API. I have register an application and get API id. But I am not able to find accountId and clientFolderId.
Please see this below link :
http://developer.icontact.com/documentation/request-your-accountid-and-clientfolderid/
At above page "Perform a GET on the Accounts resource" How I can perform this to get account id and clientfolderid.
The easiest way I have found: Login to sandbox or to your real iContact account, in the main menu go to Contact -> Sign-up forms, then create just any form, click on view HTML and you will find Account Id there.
This is my full code to get the account id and client folder id, thanks to Carlos Duran above for getting some of my code problems worked out:
/* iContact LIVE * /
$GLOBALS['iContact_settings'] = array(
'apiUrl' => 'https://app.icontact.com',
'apiPage' => '/icp/a/',
'username' => 'username',
'password' => 'password',
'appId' => 'appId'
);
/* iContact SANDBOX */
$GLOBALS['iContact_settings'] = array(
'apiUrl' => 'https://app.sandbox.icontact.com',
'apiPage' => '/icp/a/',
'username' => 'username-beta',
'password' => 'password',
'appId' => 'appId'
);
/**/
$icontact_url = $GLOBALS['iContact_settings']['apiUrl'] . $GLOBALS['iContact_settings']['apiPage'];
$icontact_page = $GLOBALS['iContact_settings']['apiPage'];
$icontact_headers = array(
"GET ".$icontact_page." HTTP/1.0",
"Accept: text/xml",
"Content-Type: text/xml",
"API-Version: 2.2",
"API-AppId: " . $GLOBALS['iContact_settings']['appId'],
"API-Username: " . $GLOBALS['iContact_settings']['username'],
"API-Password: " . $GLOBALS['iContact_settings']['password']
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $icontact_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $icontact_headers);
$data = curl_exec($ch);
curl_close($ch);
$account_id = "";
if (($pos=strpos($data,"<accountId>"))!==false){
$account_id = substr($data, strlen("<accountId>")+$pos);
if (($pos=strpos($account_id,"<"))!==false){
$account_id = substr($account_id, 0, $pos);
}
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $icontact_url ."$account_id/c/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $icontact_headers);
$data = curl_exec($ch);
curl_close($ch);
$client_folder_id = "";
if (($pos=strpos($data,"<clientFolderId>"))!==false){
$client_folder_id = substr($data, strlen("<clientFolderId>")+$pos);
if (($pos=strpos($client_folder_id,"<"))!==false){
$client_folder_id = substr($client_folder_id, 0, $pos);
}
}
I just switched to JSON, way better.
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $icontact_url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_HTTPHEADER, $icontact_headers);
$data = curl_exec($handle);
curl_close($handle);
$decoded = json_decode($data);
$account_id = $decoded->accounts[0]->accountId;
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $icontact_url ."$account_id/c/");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_HTTPHEADER, $icontact_headers);
$data = curl_exec($handle);
curl_close($handle);
$decoded = json_decode($data);
$client_folder_
id = $decoded->clientfolders[0]->clientFolderId;
And use:
"Accept: application/json",
"Content-Type: application/json",
Instead of text/xml above.
The only way to work with the iContact API, is to send correct headers to the server, then you will be able to make any of the requests and actions that appear in the documentation.
The best way that I found to do this is by setting up a PHP script with cUrl
$url = "https://app.sandbox.icontact.com/icp/a/";
$page = "/icp/a/";
$headers = array(
"GET ".$page." HTTP/1.0",
"Accept: text/html",
"Content-Type: text/html",
"API-Version: 2.2",
"API-AppId: yourapiappid",
"API-Username: yourapiusername",
"API-Password: yourappidpassword"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
Here you get your accountId and the rest is just calling the right url with this script!
Hope it gives you a hint.
"Keep up the good coding."
This PHP iContact API is rather useful https://github.com/icontact

Resources