我正在开发一个安卓应用程序,其中包含通过ti.cloudpush模块发送的gcm推送通知。我想为某些类型的推送通知启动特定窗口。你有什么正确的方法来解决这个问题吗?以下是我的锻炼。
CloudPush.addEventListener('callback', function(evt) {
alert("Notification received: " + evt.payload);
//-------------------launch code
var win=Ti.UI.createWindow({
url:'music.js',
exitOnClose:true,
});
});
我还尝试通过创建挂起的意图。这也不是失败。提前致谢
在回调中,您将有一个发回的有效负载。
alert(evt.payload);
CloudPush.addEventListener('callback', function(evt) {
Ti.API.debug(evt.payload);
}
有效负载是数据有效负载的 JSON 字符串。使用 JSON.parse 将其转换为您可以使用的对象。
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
您可以使用此有效负载根据要触发的意图、窗口或操作进行检查。
var payload = JSON.parse(evt.payload);
if (payload... ) {
// Do first option
} else {
// Do fallback option
}
在 cloudPush 的回调 eventEventLister 中,一旦你解析了有效负载,你可以加载类似这样的东西:
CloudPush.addEventListener('callback', function(evt) {
... Do payload check if statement from evt.payload...
// Or this could be a new window, alert, etc,
Titanium.Android.NotificationManager.notify(0,
Ti.Android.createNotification({
contentTitle : "title",
contentText : "text",
tickerText : "custom notification!",
contentIntent : Titanium.Android.createPendingIntent({
intent : Titanium.Android.createIntent({
url : 'foo.js'
})
})
})
);
});
如果要切换其他窗口,请将 目的中的 url 对象设置为自定义变量,该变量根据发送回的 evt.payload 是什么进行设置。
例如
intent : Titanium.Android.createIntent({
url : presetWindowUrl
})