android推送需要什么权限,要在Android Manifest中加入GCM推送通知的权限

试试这个代码:

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

这适用于我。希望这可以帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值