试试这个代码:
app.js类:
/*
Push notifications through device token.
Steps :
1) Retrieve device token
2) Subscribe for receiving notifications
3) Notify
4) Unsubscribe from receiving notifications
*/
Titanium.UI.setBackgroundColor('#000');
var GcmWin = Ti.UI.createWindow({
backgroundColor : '#ccc',
title : 'Android Cloud Push Notification'
});
var CloudPush = require('ti.cloudpush');
CloudPush.debug = true;
CloudPush.enabled = true;
CloudPush.showTrayNotificationsWhenFocused = true;
CloudPush.focusAppOnPush = false;
var deviceToken;
var Cloud = require('ti.cloud');
Cloud.debug = true;
var submit = Ti.UI.createButton({
title : 'Retrieve Device token',
color : '#000',
height : 80,
width : 200,
top : 50
});
var subscribe = Ti.UI.createButton({
title : 'Subscribe',
color : '#000',
height : 80,
width : 200,
top : 150
});
subscribe.addEventListener('click', subscribeToChannel);
GcmWin.add(subscribe);
var notify = Ti.UI.createButton({
title : 'Notify',
color : '#000',
height : 80,
width : 200,
top : 250
});
notify.addEventListener('click', sendTestNotification);
GcmWin.add(notify);
var unsubscribe = Ti.UI.createButton({
title : 'Unsubscribe',
color : '#000',
height : 80,
width : 200,
top : 350
});
unsubscribe.addEventListener('click', unsubscribeToChannel);
GcmWin.add(unsubscribe);
GcmWin.add(submit);
submit.addEventListener('click', function(e) {
CloudPush.retrieveDeviceToken({
success : function deviceTokenSuccess(e) {
alert('Device Token: ' + e.deviceToken);
deviceToken = e.deviceToken;
},
error : function deviceTokenError(e) {
alert('Failed to register for push! ' + e.error);
}
});
});
function subscribeToChannel() {
// Subscribes the device to the 'test' channel
// Specify the push type as either 'android' for Android or 'ios' for iOS
Cloud.PushNotifications.subscribeToken({
device_token : deviceToken,
channel : 'test',
type : Ti.Platform.name == 'android' ? 'android' : 'ios'
}, function(e) {
if (e.success) {
alert('Subscribed');
} else {
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
}
function sendTestNotification() {
// Sends an 'This is a test.' alert to specified device if its subscribed to the 'test' channel.
Cloud.PushNotifications.notifyTokens({
to_tokens : deviceToken,
channel : 'test',
payload : 'This is a test.'
}, function(e) {
if (e.success) {
alert('Push notification sent');
} else {
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
}
function unsubscribeToChannel() {
// Unsubscribes the device from the 'test' channel
Cloud.PushNotifications.unsubscribeToken({
device_token : deviceToken,
channel : 'test',
}, function(e) {
if (e.success) {
alert('Unsubscribed');
} else {
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
}
CloudPush.addEventListener('callback', function(evt) {
//alert(evt);
alert(evt.payload);
});
CloudPush.addEventListener('trayClickLaunchedApp', function(evt) {
Ti.API.info('Tray Click Launched App (app was not running)');
//alert('Tray Click Launched App (app was not running');
});
CloudPush.addEventListener('trayClickFocusedApp', function(evt) {
Ti.API.info('Tray Click Focused App (app was already running)');
//alert('Tray Click Focused App (app was already running)');
});
在tiapp.xml只是检查这下面lines.Add,如果不是有:
ti.cloud
ti.cloudpush
这适用于我。希望这可以帮助。