Sending Push Notifications to IOS Devices with php - php

I'm working on and IOS App that will send push notifications with FCM, i've configured everything right on the FCM Console and tested sending a message from the console to the device and it worked. Now i'm using the below script and the message is not able to push to the phone.
When i send i get this message
{"multicast_id":7893466200486146313,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1506525095628728%b54fead7f9fd7ecd"}]}
Below is my script
// Replace with the real server API key from Google APIs
$apiKey = "-------";
// Replace with the real client registration IDs
$registrationIDs = array('00000');
// Message to be sent
$message = "New Message from GWCL";
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( 'message' => $message,'title' => "New Message",'notId'=>''.time(), 'complain_id'=>'90','push'=>'reply','priority'=>'high'),
);
$headers = array(
'Authorization: key=' . $apiKey,
'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));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
I followed a tutorial which told me to add 'priority'=>'high', i did but still didn't work

Recently i worked on Push notification on both Firebase and APN so i can say as per your FCM response Push notification sent successfully from PHP end
so I guess you are handling some think wrong in your app side
As i am PHP developer i am not much aware of ios development.so can share some links it might help you to debug
firebase iOS not receive push notifications
Firebase when receive Push Notification did not receive the popup
iOS not receiving Firebase Push Notification sent through the API
Not receiving Push Notifications Using firebase and Objective C
Not receiving Firebase push notifications when app is open?

Related

how to send push notification from onesignal the device id's are stored in database - cordova app

I'm using push notification from onesignal platform in my cordova app. I have implemented it to send push notification from oneSignal dashboard but I want to send notification from my own search, I have stored device ids when user install the app but don't know how to send notifications to devices that's ids are stored in my database
Any help regarding this will be appreciated . Thanks if anyone has issue to understand my question can comment i will explain with my best capacity.
To handle this case, I also use one signal, but at the launch of my app, I send my id to my backend to be sure it will be registered to my db when I will need it. Just with a classic http post request.
There is no magic trick to achieve that, you don't have lot of choices here except save it to your db by a way or another.
You can use this function which is provided by OneSignal
function sendMessage(){
$content = array(
"en" => 'YOUR_CUSTOM_MESSAGE'
);
$fields = array(
'app_id' => "YOUR_APP_ID",
'include_player_ids' => array("YOUR_TARGET_ID"),
'contents' => $content
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode( $return);
print("\n\nJSON received:\n");
print($return);
print("\n");

Firebase Cloud Messaging - PHP REST API not working on iOS

I'm using below PHP code to send push notification to Android and iOS devices using Firebase REST API. Push notifications are coming fine in Android device. But it is not coming in iOS device.
Meanwhile, when I send the notification from the Firebase console, both devices are receiving it. Did I miss anything in my API implementation?
$data = array("to" => "/topics/news",
'priority' => '10',
'notification' => array('body' => $msg));
$headers = array
(
'Authorization: key='.$apiKey,
'Content-Type: application/json'
);
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ouput = curl_exec($ch);
if ($ouput === false) {
throw new Exception(curl_error($ch), curl_errno($ch));
}
$response = curl_getinfo($ch);
} catch(Exception $e) {}
You should set your priority to high instead of 10.
When using GCM/FCM, the only valid values for priority are normal and high which is equal to 5 and 10 for iOS APNs, as was mentioned here:
Sets the priority of the message. Valid values are "normal" and "high." On iOS, these correspond to APNs priorities 5 and 10.

Sending Push notification to android Using PHP :curl failed

I am trying to send push notification some android devices using the registration ids which is stored in database table.
Here is my code
//message and title for push notification
$msg=$_POST['message'];
$title=$_POST['title'];
$result=$connect->prepare("SELECT `push_id` FROM `user_devices`");
$result->execute();
//get registration id's
$ids=$result->fetchAll(PDO::FETCH_COLUMN);
$key="XXXXX";
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' =>$ids,
'data' => array( "message" =>$msg,"title"=>$title,"soundname"=>"beep.wav" ),
);
$headers = array(
'Authorization: key=' . $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, json_encode( $fields ) );
$res = curl_exec($ch);
if($res === false)
echo 'Curl failed ' .curl_error();
}
I am getting curl Failed error message but didn't see anything from curl_error().
UPDATE
I am running this script from windows server which is hosted in azure cloud service.
Now i am run this from linux based server its works fine,but it didnt work in windows server.
You are probably getting error due to SSL (in your url https) issue. You have to configure your curl to handle secured request. You can find a hints from here: Configuring cURL for SSL
You are not getting the error msg because you need to print it like below:
echo 'Curl failed ' .curl_error($ch); // you are missing $ch here
Alternately you can run your curl with verbose enabled to see the things happening with your curl request!
curl_setopt( $ch, CURLOPT_VERBOSE, true );
It was the problem of My SSL Certificate.
Solved using one line code
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); is placed in after curl_init.
NOTE
This is a temporary solution.Actual way is generate an SSl certificate.

testing gcm push notification in android

I used mark nutter phonegap plugin for GCM and I successfully get the registration id back from server
here is php script that I use
<?php
// Replace with real server API key from Google APIs
$apiKey = "AIzaSyC7wzEjjMaOGLYp8_w3USftv_3lI-qCdZ4";
// Replace with real client registration IDs
$registrationIDs = array( "APA91bFt3slFE96jaB5qnoD5gR0TCwsxe5StEGyrECR0umYviG0cfG1JNnFxYqP1ERr1RoWc38rsuWjRUx5SZ7cgUNG9-mQ4mSsY8_XQLquft5DLnqWcLCEB2wtQpfA6EAp5OQmOWzpUZU5bohfG4sfLWNUco7XxXg");
// Message to be sent
$message = "hi SOurabh";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . $apiKey,
'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 ) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_POST, true);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
//print_r($result);
//var_dump($result);
?>
I do not know much about php I used thi just to test notifications
and I get following message when I run this
{"multicast_id":9187695352946100598,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}
any idea why do I get this?
or any other way to test the app?
Does the project ID that you used on the project match the one shown on the google console ?
https://code.google.com/apis/console/#project:?????
Does the API key belong under 'server apps' on the google GCM page ? Your phone must also have a valid registered google account to receive notifications.
I started to use your php-script to debug push notification on my app.
First time I received the same error.
Updating SENDER_ID value to Google API Console App Project ID in Android client application fixed the issue - messages are delivered successfully.

Sending notifications to Google Cloud Messaging with php gives me Unauthorized Error 401

Searching for some info about how to send notifications using GCM but with PHP instead of servlets, i found this: GCM with PHP (Google Cloud Messaging)
I tested the working code of the responses of these questions, also i created a Key for browser apps (with referers), and i give permissions to this ip: .mywebsite.com/ (te php file is on this url: "http://www.mywebsite.com/~jma/cHtml5/cap/kk.php")
But i'm getting this response: Unauthorized Error 401
What i'm doing wrong?
this is the php file:
<?php
// Replace with real server API key from Google APIs
$apiKey = "fictional key";
// Replace with real client registration IDs
$registrationIDs = array( "APA91asdasdSDGGS232S13S4213abGqiNhCIXKjlxrkUYe_xTgTacNGB5n16b380XDd8i_9HpKGRHkvm8DDet4_WK3zumjDEKkTRWLgPS7kO-BrKzWz7eWFQaDD9PJ8zA6hlSqL9_zH21P8K22ktGKmo_VIF6YAdU9ejJovrKBTpgQktYkBZBf9Zw","APAasdasd32423dADFG91bHYYxYB7bFiX5ltbJt6A-4MBiNg7l4RS4Bqf3jIfYviaaUfZ810XJo2o66DY9-jdeJk_JR8FIZCyrmCv-eu_WLkGZ8KaoHgEDR_16H2QPm98uHpe1MjKVXbzYc4J89WMmcIrl5tHhWQnIQNzaI6Zp6yyFUNUQ");
// Message to be sent
$message = "Test Notificación PHP";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . $apiKey,
'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 ) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
//print_r($result);
//var_dump($result);
?>
Solved!!!
you must use Key for server apps (with IP locking) instead of browser key
:)
You need to use the key for server apps in the field of API key in your server side coding.
While creating the server key don't enter anything inside IP address field.

Categories