简体   繁体   English

Titanium-Android-当应用程序不在前台时,推送通知失败

[英]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.

相关问题 当应用程序在后台(Titanium)时,Android 5上的推送通知未送达 - 但它确实在前台 - Push notifications on Android 5 not delivered when app in background (Titanium) - But it does in foreground 使用Delphi处理应用不在前台时的Android Push通知 - Handling Android Push notifications when app is not in the foreground using Delphi 当App位于前台时,FCM推送通知将被替换 - FCM Push Notifications are replacing when App is in Foreground Android 中的推送通知仅在 PhoneGap 应用程序使用节点推送通知处于前台时才有效 - Push notifications in Android only work when PhoneGap app is in foreground using node-pushnotifications Android-通过GCM在前台/后台使用应用发送推送通知 - Android - Sending Push Notifications via GCM with app in foreground/background 钛工作室推送通知关闭该应用程序 - titanium studio push notifications closing the app Appcelerator / Titanium:获取Android凭据以推送通知 - Appcelerator/ Titanium: Getting Android credentials to push notifications 从Titan应用程序更新到Android SDK时无法更新apk - Fail to update apk when updating from titanium app to Android SDK 应用被杀死时的Android推送通知 - Android push notifications when app is killed 将通知推送到Android应用 - Push Notifications to Android App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM