[英]Titanium - Android - Push Notifications Fail When App Is Not In Foreground
Titanium SDK version 3.4.0.GA Targeting Android SDK: 20 Titanium SDK版本3.4.0.GA Targeting Android SDK:20
Debugging on device running Android 4.4.4 在运行Android 4.4.4的设备上进行调试
Am able to retrieve deviceToken and can receive push notifications. 能够检索deviceToken并可以接收推送通知。
But the push notifications show up as an alert only when the application has focus 但是,仅当应用程序具有焦点时,推送通知才会显示为警报
When the application does not have focus, I get an error that says the application has stopped working. 当应用程序没有焦点时,我收到一条错误消息,指出该应用程序已停止工作。
Sometimes, bringing the application back into focus shows the notifications sent when the application was not in focus. 有时,使应用程序重新变得集中起来会显示在应用程序不集中时发送的通知。
How do start to debug this, please? 请如何开始调试呢?
A little fogged - any pointers would be a great help 有点迷雾-任何指针都会有很大帮助
Many thanks 非常感谢
Iyer 艾耶
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');
var df = require("./dreamfactory");
var promise = require("./Promise");
var winMain = Ti.UI.createWindow({
backgroundColor:"#000",
layout: "vertical"
});
var CloudPush = require('ti.cloudpush');
var deviceTokenLabel = Ti.UI.createLabel({
top: '10dp', width: '320dp', height: (CloudPush.pushType=='gcm'?'150dp':'40dp'),
font: { fontSize:14}, color: 'white',
text: 'Device Token'
});
winMain.add(deviceTokenLabel);
var deviceToken = Ti.App.Properties.getString("deviceToken", "");
if (deviceToken == "") {
CloudPush.retrieveDeviceToken({
success: deviceTokenSuccess,
error: deviceTokenError
});
} else {
deviceTokenLabel.text = 'Already Had Device Token:' + deviceToken;
registerWithDSP();
}
function deviceTokenSuccess(e) {
Ti.API.info('Device Token: ' + e.deviceToken);
deviceTokenLabel.text = 'Device Token:' + e.deviceToken;
Ti.App.Properties.setString('deviceToken', e.deviceToken);
registerWithDSP();
//enablePush.enabled = true;
}
function registerWithDSP() {
var body = {email:"iyer@ndtv.com" , password : "yeggikallie"};
body = JSON.stringify(body);
Ti.API.info(body);
df.makeRequest("post","/user/session", body).then(function(response) {
SESSION_ID = response.session_id;
Ti.API.info("session id " + SESSION_ID);
}, function(error){
Ti.API.info("Could not connect " + error);
});
}
function deviceTokenError(e) {
alert('Failed to register for push! ' + e.error);
deviceTokenLabel.text = 'Failed to get device token.';
}
CloudPush.addEventListener('callback', function (evt) {
alert(evt.payload);
});
CloudPush.addEventListener('trayClickLaunchedApp', function (evt) {
Ti.API.info('Tray Click Launched App (app was not running)');
});
CloudPush.addEventListener('trayClickFocusedApp', function (evt) {
Ti.API.info('Tray Click Focused App (app was already running)');
});
winMain.open();
Depending on how you are sending your push notifications, it could be a malformed payload (which was my issue). 根据您发送推送通知的方式,可能是有效载荷格式错误(这是我的问题)。 I'm using PHP and my code is below:
我正在使用PHP,代码如下:
$data = array(
'title' => 'Your App Title',
'sound' => 'default',
'alert' => 'Your message here',
'vibrate' => true );
$fields = array(
'registration_ids' => array('YOUR DEVICE TOKEN']),
'data' => array('payload'=> array('android' => $data)),
'aps' => $data );
$headers = array(
'Authorization: key=' . 'YOUR API KEY',
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
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_POSTFIELDS, json_encode( $fields ) );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.