iOS empty Firebase Push notification doesn't show up - ios

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.

Related

Replicate CURL multipart/form-data request with Guzzle / League OAuth2 Client

I am attempting to replicate a CURL POST request in Guzzle, but Guzzle request is failing.
This is the CURL request that works successfully:
$file = new \CURLFile( $document );
$file->setPostFilename( basename( $document ) );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_URL, $endpoint );
curl_setopt( $ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer " . $accessToken,
"Content-Type: multipart/form-data",
] );
curl_setopt( $ch, CURLOPT_POSTFIELDS, [ 'fileData' => $file ] );
$response = curl_exec( $ch );
And here is what I am currently using for the Guzzle request but it does not work:
$options['multipart'][] = [
'name' => 'fileData',
'contents' => fopen( $document, 'r' ),
'filename' => basename( $document ),
];
$request = $provider->getAuthenticatedRequest( 'POST', $endpoint, $accessToken, $options );
$response = $provider->getParsedResponse( $request );
The response from the Guzzle request is as follows:
{"message":"File cannot be empty","errors":[{"code":"Missing","fields":["document"]}]}
It's worth noting I am using the thephpleague/oauth2-client library to send the request. I'm looking for any discrepencies between the two request or information on how I can troubleshoot this further myself as I have been spinning my wheels all day on this. Much appreciated
thephpleague/oauth2-client uses different Providers to create the requests and these Providers implement AbstractProvider
The argument $options of AbstractProvider 's getAuthenticatedRequest() is different from GuzzleHttp\Client 's request():
/**
* Returns an authenticated PSR-7 request instance.
...
* #param array $options Any of "headers", "body", and "protocolVersion".
* #return RequestInterface
*/
public function getAuthenticatedRequest($method, $url, $token, array $options = [])
Only keys allowed for $options are "headers", "body", and "protocolVersion".
You should do the extra effort and create the headers and body needed:
$file = new \CURLFile( $document );
$file->setPostFilename( basename( $document ) );
$data = array(
'uploaded_file' => $file
);
$options = array(
'headers' => array("Content-Type" => "multipart/form-data"),
'body' => $data
);
$request = $provider->getAuthenticatedRequest( 'POST', $endpoint, $accessToken, $options );
Reference
Send file via cURL from form POST in PHP

Sending Firebase push notifications from Laravel

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
);

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.

GCM push notifications not working for iOS when the app is not active

I have enabled the background modes in xcode and checked "Remote notifications".
Do I have to enable "Push Notifications" too? for GCM
here is the payload from my rest client
{
"data": {
"displayMessage": {
"message": "Package delivered",
}
},
"registration_ids": [
"dgfdghdfghgfhhfjhgjghjghjhjghjghjghjghjghjghjhgggh"
],
"content-available" : true,
"priority": "high"
}
This works when the app is in foreground and does not work when the app is not in foreground.
Any help to get this GCM for ios working would be highly appreciated.
I would suggest to check the format of the message as it is mentioned here :
Making GCM work for iOS device in the background
Your key is wrong. Use content_available, not content-available.
Also, your JSON is invalid. Specifically, this:
"message": "Package delivered",
should not have a comma at the end.
This issue occurred from server side. Notification message body should be like this.
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"content_available" : "true"
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark"
}
}
Here is the code that I have implemented and working fine.
<?php
// Message to send
$notificationTitle = "Notification title";
$notificationtBody = "Notification arrived";
$registrationId = 'GCM_DEVICE_TOKEN';
$apiKey = "GCM_SERVER_KEY";
$response = sendNotification(
$apiKey,
$registrationId,
array(
'sound' => "default",
'badge' => 1,
'body' => $notificationtBody,
'title' => $notificationTitle
)
);
echo $response;
function sendNotification( $apiKey, $registrationId, $messageData )
{
$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
$data = array(
'to' => $registrationId,
'content_available' => true,
'notification' => $messageData
);
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
This notification worked for me
{
"to":"ID",
"notification":{
"sound":"default",
"title":"TITLE",
"body":"BODY"
},
"priority": "high"
}

post data with curl and change page url

I have a shopping site and I have to send username and password and some other data to the bank, so I have used curl for posting data .because it should be safe.
but my problem is when I post data with curl to the bank, page url dont change! I see banks result in my server page !!!
I need this posting data same as posting data in html page redirect too the url which has been determined !!
$data = array(
"MID" => "0113-19",
"RedirectURL" => $SERVER . "/shop/profile.php",
"Amount" => $sum,
"ResNum" => $ResNum,
"username" => "xxxx",
"password" => "1234");
//traverse array and prepare data for posting (key1=value1)
foreach ( $data as $key => $value) {
$data[] = $key . '=' . urlencode($value);
}
$post_string = implode ('&', $data);
$res = array();
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_POST => true, // return web page
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_AUTOREFERER => false, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_POSTFIELDS => $post_string, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false , // stop after 10 redirects
CURLOPT_COOKIEJAR => "cookie.txt" , // stop after 10 redirects
CURLOPT_COOKIEFILE => "cookie.txt" , // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false , // stop after 10 redirects
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U;
Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1" // stop after 10 redirects
);
//////////////////////
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );

Resources