使用钛合金解析推送通知

时间:2014-11-18 10:21:43

标签: android push-notification titanium

我正在为android / Ios构建移动应用程序,对于android,需要使用parse的推送通知来传递我的消息并获取我的请求。我得到了我的Android设备的设备令牌,我可以列出我的收件人,但如果我点击发送按钮我的消息没有交付。这是我的代码,我有     var uuid = Ti.Platform.createUUID();

var deviceToken;
var CloudPush = require('ti.cloudpush');
//fetch device token
CloudPush.retrieveDeviceToken({
success : function deviceTokenSuccess(e) {
deviceToken = e.deviceToken;
//alert('Device Token: ' + deviceToken);
Ti.API.info('Device Token: ' + e.deviceToken);
loginDefault();
/////////parse start///
var c = Titanium.Network.createHTTPClient();
c.setTimeout(25000);
c.onload = function(e) {
Ti.API.info("onload");
svar = JSON.parse(this.responseText);
Ti.API.info(svar);
Ti.API.info('params'+this.responseText);
};
c.onerror = function(e) {
Ti.API.info("on error");
alert(e);
};
c.open('POST', 'https://api.parse.com/1/installations');
c.setRequestHeader('X-Parse-Application-Id', 'bopIfF9m4JpkAxww9syYvLHVaCmE2go9WW7uHS1K');
c.setRequestHeader('X-Parse-REST-API-Key', 'NZLlV86V8ruxq5GdXRi2NrepQXhyiSmmoDHeZasH');
c.setRequestHeader('Content-Type', "application/json; charset=utf-8");
var params = {
"deviceType": "android",
    "deviceToken": deviceToken,
    "installationId":uuid,
    "pushType":"gcm",
 };
 Ti.API.info('value is'+params);
c.send(JSON.stringify(params)); 
///parse end/////
},
 error : function deviceTokenError(e) {
 // alert('Failed to register for push! ' + e.error);
    }
});
CloudPush.debug = true;
CloudPush.enabled = true;
CloudPush.showTrayNotificationsWhenFocused = true;
CloudPush.focusAppOnPush = false;
var Cloud = require('ti.cloud');
Cloud.debug = true;
function loginDefault(e) {
//Create a Default User in Cloud Console, and login with same credential
Cloud.Users.login({
            login : 'push1',
            password : '12345'
    }, 
function(e) {
            if (e.success) {
                   // alert("Login success");
                    defaultSubscribe();
            } else {
                  //  alert('Login error: ' + ((e.error && e.message) || JSON.stringify(e)));
            }
    });
}
function defaultSubscribe() {
    Cloud.PushNotifications.subscribe({
            channel : 'alert',//'alert' is channel name
            device_token : deviceToken,
            type : 'gcm' //here i am using gcm, it is recomended one
    }, function(e) {
            if (e.success) {
                 // alert('Subscribed for Push Notification!');
            } else {
                  //alert('Subscrib error:' + ((e.error && e.message) || JSON.stringify(e)));

            }
    });
}
CloudPush.addEventListener('callback', function(evt) {
  //  alert(evt.payload);
  // alert("ggg");
});
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)');
});  

1 个答案:

答案 0 :(得分:0)

更改

c.setRequestHeader('Content-Type', "application/json; charset=utf-8");

c.setRequestHeader('Content-Type', "application/json");