Last active
August 30, 2019 18:30
Sending push notification using php server.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Cloud = require("ti.cloud"); | |
Cloud.Users.login({ | |
login : 'YOUR_LOGIN', | |
password : 'password' | |
}, function(e) { | |
if (e.success) { | |
var user = e.users[0]; | |
Ti.API.info('Success:\n' + 'id: ' + user.id + '\n' + 'sessionId: ' + Cloud.sessionId + '\n' + 'first name: ' + user.first_name + '\n' + 'last name: ' + user.last_name); | |
} else { | |
Ti.API.info('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); | |
} | |
}); | |
var win = Ti.UI.createWindow({ | |
backgroundColor : '#fff' | |
}); | |
// Create a Button. | |
var subscribed = Ti.UI.createButton({ | |
title : 'subscribed', | |
height : Ti.UI.SIZE, | |
width : Ti.UI.SIZE, | |
top : 20, | |
}); | |
// Listen for click events. | |
subscribed.addEventListener('click', function() { | |
subscribeToChannel(); | |
}); | |
// Add to the parent view. | |
win.add(subscribed); | |
var deviceToken = null; | |
var CloudPush = require('ti.cloudpush'); | |
// Initialize the module | |
CloudPush.retrieveDeviceToken({ | |
success : deviceTokenSuccess, | |
error : deviceTokenError | |
}); | |
// Enable push notifications for this device | |
// Save the device token for subsequent API calls | |
function deviceTokenSuccess(e) { | |
deviceToken = e.deviceToken; | |
} | |
function deviceTokenError(e) { | |
Ti.API.info('Failed to register for push notifications! ' + e.error); | |
} | |
// Process incoming push notifications | |
CloudPush.addEventListener('callback', function(evt) { | |
Ti.API.info("Notification received: " + evt.payload); | |
}); | |
// Check if the device is running iOS 8 or later | |
// Process incoming push notifications | |
function receivePush(e) { | |
Ti.API.info('Received push: ' + JSON.stringify(e)); | |
} | |
// Save the device token for subsequent API calls | |
function deviceTokenSuccess(e) { | |
deviceToken = e.deviceToken; | |
} | |
function deviceTokenError(e) { | |
Ti.API.info('Failed to register for push notifications! ' + e.error); | |
} | |
function subscribeToChannel() { | |
// Subscribes the device to the 'news_alerts' channel | |
// Specify the push type as either 'android' for Android or 'ios' for iOS | |
Cloud.PushNotifications.subscribe({ | |
channel : 'update_alert', | |
device_token : deviceToken, | |
type : Ti.Platform.name == 'android' ? 'android' : 'ios' | |
}, function(e) { | |
if (e.success) { | |
alert('Success'); | |
} else { | |
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); | |
} | |
}); | |
} | |
win.open(); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*** SETUP ***************************************************/ | |
$key = "YOUR_APP_KEY"; | |
$username = "YOUR_LOGIN"; | |
$password = "YOUR_PASSWORD"; | |
$channel = "PUSH_CHANNEL"; | |
$to_ids = "everyone"; | |
$message = "YOUR_MESSAGE"; | |
$title = "YOUR_ANDROID_TITLE"; | |
$tmp_fname = 'cookie.txt'; | |
$json = '{"alert":"'. $message .'","title":"'. $title .'","vibrate":true,"sound":"default"}'; | |
if (!is_null($key) && !is_null($username) && !is_null($password) && !is_null($channel) && !is_null($message) && !is_null($title)){ | |
/*** PUSH NOTIFICATION ***********************************/ | |
$post_array = array('login' => $username, 'password' => $password); | |
/*** INIT CURL *******************************************/ | |
$curlObj = curl_init(); | |
$c_opt = array(CURLOPT_URL => 'https://api.cloud.appcelerator.com/v1/users/login.json?key='.$key, | |
CURLOPT_COOKIEJAR => $tmp_fname, | |
CURLOPT_COOKIEFILE => $tmp_fname, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_POST => 1, | |
CURLOPT_POSTFIELDS => "login=".$username."&password=".$password, | |
CURLOPT_FOLLOWLOCATION => 1, | |
CURLOPT_TIMEOUT => 60); | |
/*** LOGIN **********************************************/ | |
curl_setopt_array($curlObj, $c_opt); | |
$session = curl_exec($curlObj); | |
/*** SEND PUSH ******************************************/ | |
$c_opt[CURLOPT_URL] = "https://api.cloud.appcelerator.com/v1/push_notification/notify.json?key=".$key; | |
$c_opt[CURLOPT_POSTFIELDS] = "channel=".$channel."&payload=".$json."&to_ids=".$to_ids; | |
curl_setopt_array($curlObj, $c_opt); | |
$session = curl_exec($curlObj); | |
/*** THE END ********************************************/ | |
curl_close($curlObj); | |
header('Content-Type: application/json'); | |
die(json_encode(array('response' => json_decode($session)))); | |
}else{ | |
header('Content-Type: application/json'); | |
die(json_encode(array('response' => json_decode($session)))); | |
} | |
?> |
I think the problem is caused by the variable tmp_fname. I have read / write permission to my server. I'm still getting the same error.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello, i am trying the code. i am getting an error:
{"response":{"meta":{"status":"fail","code":400,"message":"Failed to authenticate user"}}}