I am Implementing push notification in ionic using this tutorial: http://devdactic.com/ionic-push-notifications/
I have succesfully done using this.
I got device token: like:
Got token: DEV-f30dac7a-d761-4a4c-a579-01918b9662ad none
When I use this in following php script:
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'apikey' );
$registrationIds = array( 'f30dac7a-d761-4a4c-a579-01918b9662ad' );
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/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 );
echo $result;
I got error like:
{`"multicast_id":6920644147476475688,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}`
I dont Know where I am wrong.Please Help
The error says that your app is not registred. Make sure you put your GCM API key in 'apikey'.
The token is DEV-f30dac7a-d761-4a4c-a579-01918b9662ad, and not f30dac7a-d761-4a4c-a579-01918b9662ad as you put in $registrationIds.
Related
i want to give online test with xui one api via sms api contact form how can i do this
http://dns/accescode/?api_key=*****&action=create_line&is_trial=1&username=trialeapi&exp_date=1hour&bouquet=???&allowed_outputs=???
Hello
i want to give online test with xui one api via sms api contact form how can i do this
ex: curl "array( 'username' => $username, 'password' => $password, 'max_connections' => $max_connections, 'is_restreamer' => $reseller, 'exp_date' => $expire_date, 'bouquet' => json_encode( $bouquet_ids ) ) ); $opts = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query( $post_data ) ) ); $context = stream_context_create( $opts ); $api_result = json_decode( file_get_contents( $panel_url . "api.php?action=user&sub=create", false, $context ) ); ?>"
Example (API Success)
ex: curl "{"result":true,"created_id":14838,"username":"d4PSc5uCqF","password":"2ZiuRRZk4b"}"
The API returned as the username/password of the NEW line, as well as the ID so we can use it wherever we want.
Example(API Failed)
ex: curl "{"result":false,"error":"EXISTS"} {"result":false,"error":"PARAMETER ERROR"}"
I am trying to integrate a Stripe payment method in a web application. I am stuck: payment_init.php does not load, when I am redirected to the page. I get 403 Forbidden error code ("Forbidden. You don't have permission to access this resource.
Additionally, a 400 Bad Request error was encountered while trying to use an ErrorDocument to handle the request").
Here is my payment_init.php file's code:
<?php
// Include the Stripe PHP library
require_once 'stripe-php/init.php';
// Include the configuration file
require_once 'config.php';
$chosenService = $_POST['submitService'];
printf($chosenService);
// Product Details
if ($chosenService === "1") {
$productName = "Hajvágás (6900 HUF)";
$productID = "hc001";
$productPrice = 6900;
} elseif ($chosenService === "2") {
$productName = 'Hajvágás + Szakáll (9900 HUF)';
$productID = "hc002";
$productPrice = 9900;
};
$currency = "huf";
$description = "20% előleg Mobil Barber";
printf($productName);
// Set API key
\Stripe\Stripe::setApiKey(STRIPE_API_KEY);
$response = array(
'status' => 0,
'error' => array(
'message' => 'Invalid Request!'
)
);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$input = file_get_contents('php://input');
$request = json_decode($input);
}
if (json_last_error() !== JSON_ERROR_NONE) {
http_response_code(400);
echo json_encode($response);
exit;
}
if (!empty($request->createCheckoutSession)) {
printf($productName);
// Convert product price to cent
$stripeAmount = round($productPrice * 100, 2);
// Create new Checkout Session for the order
try {
printf($productName);
$checkout_session = \Stripe\Checkout\Session::create([
'line_items' => [[
'price_data' => [
'currency' => $currency,
'unit_amount' => $productPrice,
'product_data' => [
'name' => $productName,
'description' => $description,
],
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => STRIPE_SUCCESS_URL . '?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => STRIPE_CANCEL_URL,
]);
} catch (Exception $e) {
$api_error = $e->getMessage();
}
if (empty($api_error) && $checkout_session) {
$response = array(
'status' => 1,
'message' => 'Checkout Session created successfully!',
'sessionId' => $checkout_session->id
);
} else {
$response = array(
'status' => 0,
'error' => array(
'message' => 'Checkout Session creation failed! ' . $api_error
)
);
}
}
// Return response
echo json_encode($response);
When I print $chosenService and $productName variables outside the "if (!empty($request->createCheckoutSession)) {...}" condition, I get the parameters, so they are not NULL. But inside the condition I do not get anything back, neither NULL (does this mean that the $request is empty?). I even checked the Logs in Stripe dashboard, this is the err message there:
"parameter_missing - line_items[0][price_data][product_data][name]
Looks like you are missing the name field tied to a given line item's product_data.
This field is required in that it contains the name that will show up on associated invoice line item descriptions."
I would be really grateful, if someone could help me with this. Thank you in advance.
I don't think your problem is the $productName. I tested out the code you provided and it looks like the issue has to do with the price value. You convert the $productPrice to $stripeAmount but then you don't use it. Without the conversion the amounts for either of the services are less than the $0.50 threshold (with the USD = HUF conversion).
As their docs point out, Stripe requires your charge amounts to be valued between $0.50 and $999,999.00
I don't think this impacted your attempt here but it might also be worth updating the way in which you are invoking/using the Stripe PHP library to conform to the current standard: https://github.com/stripe/stripe-php#getting-started
It will mean you can more easily use the code snippets displayed in the API docs
I am trying to use PHP + GCM + Service Workers to send push notifications to users of my website (not app).
//service-worker.js
self.addEventListener('push', function(event) {
console.log('Received a push message');
console.log(event.data.text());
});
When I go to the Chrome Console >> Application >> Service Workers >> Push the console shows the following:
Received a push message
Test push message from DevTools.
I am trying to accomplish the same thing using a PHP script:
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array
(
'registration_ids' => $ids,
'data' => $msg,
);
$headers = array
(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
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 );
The push notification comes through, however, I get the following:
Received a push message
Uncaught TypeError: Cannot read property 'text' of null
I've tried digging through other properties of event with no luck. Most of the help articles I have read are for Android and not for web development. What am I missing?
I'm trying to figure out about sending push notification using PHP.
The problem is, how to register service worker in browser and send notification directly from PHP.
I got sample of code for back-end part:
<?php
define( 'API_ACCESS_KEY', 'SOME KEY' );
$registrationIds = array('fK_LVMivilI:APA91bHCXawEoNXc0Avwk4F6iYsshn4mmWX6jysrg8gC9PGA6_AlqWtr1HXhIMonCCUj8syOlsDGTFJVu_T3aPqdMNynqy7SY5L9OBlgolu-7L2a5pwZrB7kN_bdnUPeZHJQ1HT2i2ed');
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/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 );
echo $result;
When I'm trying to run this script, I'm getting the following error:
{"multicast_id":7003820144951503575,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"}]}
I guess it's because there are no service workers registered in the browser.
So, I need some solution to send notification with dynamic content using PHP and javascript if needed.
Any advice appricated.
Check this documentation on how to register a Service Worker in implementing Push Messaging. It is stated here that there is a dependency of having a service worker to implement push messages for the web. The reason for this is that when a push message is received, the browser can start up a service worker, which runs in the background without a page being open, and dispatch an event so that you can decide how to handle that push message.
This is the sample code use for this.
var isPushEnabled = false;
…
window.addEventListener('load', function() {
var pushButton = document.querySelector('.js-push-button');
pushButton.addEventListener('click', function() {
if (isPushEnabled) {
unsubscribe();
} else {
subscribe();
}
});
// Check that service workers are supported, if so, progressively
// enhance and add push messaging support, otherwise continue without it.
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js')
.then(initialiseState);
} else {
console.warn('Service workers aren\'t supported in this browser.');
}
});
For more information about Service Worker, check this tutorials:
PUSH NOTIFICATIONS ON THE WEB - GOOGLE CHROME
WEB PUSH NOTIFICATION WITH SERVICE WORKER
mybb-gcm-push-notifications/service-worker.js.php
Well, I have a CakePHP 2.2.5 app which is basically management of users, and a user has a photo, and I create a PhotosController (as suggestion of this tutorial). I tested it and it works perfectly on Photos/View, including the ajax elements to be possible send a photo in a form without redirecting. And now I'm trying to connect all of this, but it ins't as easy as I think it should be.
My Photo add method:
public function add()
{
if ( $this->request->is( 'post' ) ) {
$this->Photo->create();
$json = array( 'result' => array( 'files' => array() ) );
if ( $this->Photo->save( $this->request->data ) ) {
$photo = $this->Photo->findById( $this->Photo->id );
$json['files'][0]['name'] = $photo['Photo']['basename'];
$json['files'][0]['size'] = $this->request->data['Photo']['file']['size'];
$json['files'][0]['url'] = $this->webroot . 'media/transfer/img/' . $photo['Photo']['basename'];
$json['files'][0]['thumbnail_url'] = $this->webroot . 'media/filter/thumbnail/img/' . $photo['Photo']['basename'];
}
else {
$json = 'Error';
$this->Session->setFlash( __( 'The photo could not be saved. Please, try again.' ) );
}
$this->RequestHandler->renderAs( $this , 'ajax' );
Configure::write( 'debug' , 0 );
$this->set( 'json' , json_encode( $json ) );
$this->render( '/Elements/ajax' );
}
My test View (on users controller):
<?php echo $this->Form->create(
'Photo' ,
array( 'url' => array( 'controller' => 'photos' ,
'action' => 'add'
) ,
'id' => 'fileupload' ,
'enctype' => 'multipart/form-data'
)
); ?>
<-- Some HTML HERE-->
<?php
echo $this->TB->input(
'Photo.file' ,
array(
'prepend' => 'Upload' ,
'type' => 'file' ,
'class' => 'fileUpload' ,
//'multiple' => 'multiple',
'div' => FALSE ,
'between' => FALSE ,
'after' => FALSE ,
'label' => FALSE ,
'help' => NULL ,
)
);
?>
</form>
<-- Some (already tested) Javascript to make this work HERE-->
The view renders without php errors, but when I upload a file I get an Javascript error, and the debug says:
GET http://<server>/<app>/users/add 404 (Not Found) jquery.min.js:1960
send jquery.min.js:1960
b.extend.ajax jquery.min.js:1840
(anonymous function) main.js:59
c jquery.min.js:215
p.fireWith jquery.min.js:249
b.extend.ready jquery.min.js:69
H jquery.min.js:10
Actually, I get this error on both, GET and POST methods (with some different line of jQuery of course).
Even the action is set to the PhotosController it is trying to access the Users. I think because of the view, as I din't specify users anywhere. I am trying to solve this problem for a week and I don't have more ideas.
Any help would be greatly appreciated.
Thanks in advance.