我正在将Titanium的推送通知功能与Alloy一起用于Android和iOS。我正在初始化Alloy.js的推送通知订阅,即基本上调用CloudPush的retrieveDeviceToken方法来获取设备令牌,然后
Cloud.PushNotifications.subscribe({
channel : _channel,
device_token : _token,
type : OS_IOS ? 'ios' : 'android'
}, function(_event) {..}
挑战在于,每次我重新启动应用程序时,alloy.js都会被调用。这意味着将在应用程序每次重新启动时生成一个新的设备令牌,并一次又一次地订阅频道。
我想知道这是否是使用推送订阅的正确方法。有没有一种方法可以避免来自同一设备的这些多个订阅。
实际上,您在做正确的事,但“推送通知”背后有一个事实:
参见下面的代码片段:
var CloudPush = require('ti.cloudpush');
if ( CloudPush.isGooglePlayServicesAvailable() ) {
CloudPush.retrieveDeviceToken({
success : tokenSuccess,
error : tokenError
});
// Process incoming push notifications
CloudPush.addEventListener('callback', pushRecieve);
} else {
alert("Please enable Google Play Services to register for notifications.");
}
// Save the device token for subsequent API calls
function tokenSuccess(e) {
var previousToken = Ti.App.Properties.getString("DEVICE_TOKEN", "");
var newToken = "" + e.deviceToken;
Ti.API.info('** New Device Token = ' + newToken);
if (newToken !== previousToken) {
Ti.App.Properties.setString("DEVICE_TOKEN", newToken);
var Cloud = require("ti.cloud");
Cloud.PushNotifications.subscribe({
channel : _channel,
device_token : newToken,
type : OS_IOS ? 'ios' : 'android'
}, function(_event) {});
}
}
function tokenError(e) {
alert('Failed to register for push notifications.');
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句