I'm using this library: https://github.com/davibennun/laravel-push-notification for sending push notifications. I used the example code and the push messages to my iPhone are working.
Only now I want to send 1 message to multiple devices. The docs say that I need to use:
$devices = PushNotification::DeviceCollection(array(
PushNotification::Device('token', array('badge' => 5)),
PushNotification::Device('token1', array('badge' => 1)),
PushNotification::Device('token2')
));
$message = PushNotification::Message('Message Text',array(
'badge' => 1,
'sound' => 'example.aiff',
'actionLocKey' => 'Action button title!',
'locKey' => 'localized key',
'locArgs' => array(
'localized args',
'localized args',
),
'launchImage' => 'image.jpg',
'custom' => array('custom data' => array(
'we' => 'want', 'send to app'
))
));
$collection = PushNotification::app('appNameIOS')
->to($devices)
->send($message);
// get response for each device push
foreach ($collection->pushManager as $push) {
$response = $push->getAdapter()->getResponse();
}
But I want a foreach loop from my database on the devicecollection. How do I do that with laravel? Many thanks!
foreach($usersApple as $userApple){
echo $userApple->reg_id . '<br>';
$deviceslist[] = PushNotification::Device($userApple->reg_id);
}
$devices = PushNotification::DeviceCollection($deviceslist);
Already fixed, was not sharp today haha
Related
I use Onesignal notifications for the app.
I don't have a problem sending the notifications but wanted to send these notifications using API help and this library.
php-onesignal library
The notifications I sent must be opened on the relevant page in the application. For this reason, I do not know how to write the URL in the code section below.
require_once(dirname(__FILE__).'/vendor/autoload.php');
use CWG\OneSignal\OneSignal;
$appID = '92b9c6bb-89d2-4cbc-8862-a80e4e81a251';
$authorizationRestApiKey = 'MWRjMTg2MjEtNTBmYS00ODA4LWE1M2EtM2YyZjU5ZmRkNGQ5';
$deviceID = '69aeecc1-7b58-44d1-8000-7767de437adf';
$api = new OneSignal($appID, $authorizationRestApiKey);
$retorno = $api->notification->setBody('Ola')
->setTitle('Titulo')
->addDevice($deviceID)
->send();
I entered the addTag section as in the Onesignal panel, but I could not run it.
$retorno = $api->notification->setBody('Ola')
->setTitle('Titulo')
->addTag('url', 'http://www.example.com/news/testtitle')
->send();
print_r($retorno);
How can I use it here in the Url section of the "ADDITIONAL DATA" field?
Can I solve this problem with addTag?
The following will send a notification with the $data['data'] as additional, as you want.
$data = [
'headings' => ['en' => 'Case 123'],
'contents' => ['en' => 'Case assigned ' . date('d-m-Y H:i')],
'data' => [
'type' => 'new',
'user_id' => 123,
'url' => 'http://www.example.com/news/testtitle'
],
];
OneSignal::sendNotificationCustom([
'app_id' => 1234,
'api_key' => abcd,
'included_segments' => ['All'],
'headings' => $data['headings'],
'contents' => $data['contents'],
'data' => $data['data'],
]);
I have test-driving for Mailchimp API v3 using your PHP wrapper. It's working great for me But when I'm creating a request using POST for "Create Segment" getting an error (attach screenshot):
Request Code is (through associative array) -
$api_key = "xxxxxxxxxxxxxxxx-us11";
$list_id = "1xx2xx3xx4xx";
$MailChimp = new MailChimp($api_key);
$result = $MailChimp->post('lists/' . $list_id . '/segments', array('name' => 'Testing Data',
'options' => array('match' => 'all',
'conditions' => array('field' => 'type', 'op' => 'is', 'value' => 'Testing'))
));
This request call returning following error -
array (size=2) 'field' => string 'options.conditions' (length=18)
'message' => string 'Schema describes array, object found instead'
(length=44)
I will also tried to create Request (through associative array) -
Method 1:
$api_key = "xxxxxxxxxxxxxxxx-us11";
$list_id = "1xx2xx3xx4xx";
$MailChimp = new MailChimp($api_key);
$result = $MailChimp->post('lists/' . $list_id . '/segments', array('name' => 'Testing Data',
'options' => array('match' => 'all',
'conditions' => array(array('field' => 'type', 'op' => 'is', 'value' => 'Testing')))
));
Method 2:
$api_key = "xxxxxxxxxxxxxxxx-us11";
$list_id = "1xx2xx3xx4xx";
$MailChimp = new MailChimp($api_key);
$result = $MailChimp->post('lists/' . $list_id . '/segments', array('name' => 'Testing Data 4',
'options' => array('match' => 'all',
'conditions' => array(array('field' => 'type'), array('op' => 'is'), array('value' => 'Testing')))
));
Both method will create segment on mailchimp account but not have any conditions. See screenshot -
How to override this problem?
You are missing the condition_type param. It should be selected from the list provided by MailChimp in the endpoint documentation.
For example, IF the field "type" from your MailChimp list is a text field you should use 'condition_type': 'TextMerge'. In this case, conditions should have the following format:
[
{
'condition_type': 'TextMerge',
'field': 'type',
'op': 'is',
'value': 'Testing'
}
]
However, MailChimp MAY have a bug in this endpoint, since TextMerge works only on the EMAIL field. I have also stumbled upon this problem recently:
Mailchimp api v3 - can't create segment based on a TEXT merge field
https://github.com/drewm/mailchimp-api/issues/160
I am trying to send email using AWS SES using the following PHP script:
<?php
require_once("phar://aws.phar");
use Aws\Ses\SesClient;
//Open client
$client = SesClient::factory(array(
"key" => "key",
"secret" => "secret",
"region" => "region"
));
$subject = "subject";
$messageText = "text message";
$messageHtml = "<h1>formatted message</h1>";
//Send email
try{
$response = $client->sendEmail(
array(
'Source' => 'verified_email#domain.com',
'Destination' => array(
'ToAddresses' => array('an_address#domain.com')
),
'Message' => array(
'Subject' => array('Data' => $subject),
'Body' => array('Text' => array('Data' => $messageText)),
'Html' => array('Data' => $messageHtml)
)
)
);
}catch(Exception $e){
//An error happened and the email did not get sent
echo($e->getMessage());
}
?>
Whenever I run this, it goes to the catch clause and prints to the screen the message:
Unable to determine service/operation name to be authorized
This doesn't really give me any information on what's wrong and there's no documentation on the API page about this. Any ideas?
I'm using a wamp server, and I've used this php script before when sending form values to an email..and it worked perfect before! I can't find what I've done with the code, or it might be something spooking with my wamp server, because it's not sending anymore and I get the error message: There was a problem sending your message.
Code:
// Create the form
$contactForm = new JFormer('contactForm', array(
'submitButtonText' => 'Send Message',
));
// Add components to the form
$contactForm->addJFormComponentArray(array(
new JFormComponentSingleLineText('artistname', 'Artist Name:', array(
'validationOptions' => array('required'),
'tip' => '<p>Enter your artist name.</p>'
)),
new JFormComponentSingleLineText('trackname', 'Track Name:', array(
'tip' => '<p>Enter your track name.</p>',
'validationOptions' => array('required'),
)),
new JFormComponentSingleLineText('email', 'E-mail Address:', array(
'tip' => '<p>Enter your email address.</p>',
'validationOptions' => array('required'),
)),
new JFormComponentSingleLineText('stream', 'Link to Stream:</br>(if applicable)', array(
'tip' => '<p>Enter your link to stream.</p>'
)),
new JFormComponentSingleLineText('download', 'Download Link:', array(
'tip' => '<p>Enter your download link.</p>',
'validationOptions' => array('required'),
)),
new JFormComponentMultipleChoice('multipleChoiceType', 'Full Track or Clip:',
array(
array('label' => 'Full Track', 'value' => 'fulltrack'),
array('label' => 'Clip', 'value' => 'clip'),
),
array(
'multipleChoiceType' => 'radio',
)),
new JFormComponentSingleLineText('downloadorpurchase', 'Free Download or
Purchase Link</br>(To be included in video description)', array(
'tip' => '<p>Enter your link.</p>',
)),
new JFormComponentSingleLineText('releasedate', 'Release Date:</br>(if applicable)', array(
'tip' => '<p>Enter the release date.</p>',
)),
new JFormComponentTextArea('artistandlabel', 'Artist / Label Links:</br>(To be included in description.)', array(
'validationOptions' => array('required'),
'tip' => '<p>Enter your description.</p>',
)),
new JFormComponentMultipleChoice('iagree', 'I confirm that I own full
copyright rights and grant the THU Records to post my music in their videos. I
understand that content may be monetized with adverts.',
array(
array('label' => 'I Accept', 'value' => 'accepted'),
array('label' => 'I Do Not Accept', 'value' => 'dontaccepted'),
),
array(
'iagree' => 'radio',
'validationOptions' => array('required'),
)),
));
// Set the function for a successful form submission
function onSubmit($formValues) {
// Concatenate the name
if(!empty($formValues->name->middleInitial)) {
$name = $formValues->name->firstName . ' ' . $formValues->name->middleInitial . ' ' . $formValues->name->lastName;
}
else {
$name = $formValues->name->firstName . ' ' . $formValues->name->lastName;
}
// Prepare the variables for sending the mail
$to = 'lill_z4ck3#hotmail.com';
$fromAddress = $formValues->email;
$trackName = $formValues->trackname;
$streamLink = $formValues->stream;
$download = $formValues->download;
$trackorclip = $formValues->multipleChoiceType;
$downloadOrPurchase = $formValues->downloadorpurchase;
$releaseDate = $formValues->releasedate;
$artistAndLabel = $formValues->artistandlabel;
$agreement = $formValues->iagree;
$artistName = $formValues->artistname;
$subject = "Submit from ".$formValues->artistname;
$message = "Artist Name : ".$artistName."\nTrack Name : ".$trackName."\n
Email : ".$fromAddress."\n Stream Link : ".$streamLink."\nDownload link
: ".$download."\nTrack or Clip : ".$trackorclip."\nDownload or Purchase
: ".$downloadOrPurchase."\n Release Date : ".$releaseDate."\n Artist
and Label Info : ".$artistAndLabel."\n Agreement : ".$agreement;
// Use the PHP mail function
$mail = mail($to, $subject, $message);
// Send the message
if($mail) {
$response['successPageHtml'] = '
<h1>Thanks for Contacting Us</h1>
<p>Your message has been successfully sent.</p>
';
}
else {
$response['failureNoticeHtml'] = '
There was a problem sending your message.
';
}
return $response;
}
// Process any request to the form
$contactForm->processRequest();
Do anyone of you have any idea what could be wrong? :(
The mail() function usually doesn't work by default for things like WAMP. You will need to add details of your SMTP server to the php.ini file and then configure some other settings.
This is a nice tutorial: http://roshanbh.com.np/2007/12/sending-e-mail-from-localhost-in-php-in-windows-environment.html
Hope this helps.
I am currently building a e-mail client (inbound and outbound sending) using Mandrill as the e-mail sending / inbound service and Laravel 3.x.
In order to send messages, I am using the HTTPful bundle with the Mandrill using the following code in my mail/compose POST method.
$url = 'https://mandrillapp.com/api/1.0/messages/send.json';
$data = array(
'key' => '{removedAPIkey}',
'message' => array (
'to' => array( array( "email" => $_to ) ),
'from_name' => Auth::user()->name,
'from_email' => Auth::user()->email,
'subject' => $_subject,
'html' => $_body
),
'async' => true
);
$request = Httpful::post($url)->sendsJson()->body($data)->send();
Link to better formatted code above: http://paste.laravel.com/m79
Now as far as I can tell from the API log, the request is correctly made (with the expected JSON) and a response of the following format is sent back:
[
{
"email": "test#test.com",
"status": "queued",
"_id": "longmessageID"
}
]
However, what I am trying to do is access the response from the request (specifically the _id attribute), which is in JSON. Now as far as I'm aware, the HTTPful class should do this automatically (using json_decode()). However, accessing:
$request->_id;
is not working and I'm not entirely sure how to get this data out (it is required so I can record this for soft-bounce, hard-bounce and rejection messages for postmaster-like functionality)
Any assistance would be appreciated.
Edit
Using the following code, results in the mail being sent but an error returned:
$url = 'https://mandrillapp.com/api/1.0/messages/send.json';
$data = array(
'key' => '{removedAPIkey}',
'message' => array (
'to' => array( array( "email" => $_to ) ),
'from_name' => Auth::user()->name,
'from_email' => Auth::user()->email,
'subject' => $_subject,
'html' => $_body
),
'async' => true
);
$request = Httpful::post($url)->sendsJson()->body($data)->send();
if ( $request[0]->status == "queued" ) {
$success = true;
}
Results in an exception being thrown: Cannot use object of type Httpful\Response as array
I must say, a huge thanks to Aiias for his assistance. I managed to fix this myself (I must have spent hours looking at this). For anyone who wants to know, the HTTPful bundle has a body array, where the response is kept. Therefore, the code below works:
$url = 'https://mandrillapp.com/api/1.0/messages/send.json';
$data = array(
'key' => '{removedAPIkey}',
'message' => array (
'to' => array( array( "email" => $_to ) ),
'from_name' => Auth::user()->name,
'from_email' => Auth::user()->email,
'subject' => $_subject,
'html' => $_body
),
'async' => true
);
$request = Httpful::post($url)->sendsJson()->body($data)->send();
if ( $request->body[0]->status == "queued" ) {
$success = true;
}
Again, huge thanks to Aiias for clearing some major confusion up for me!