我正在尝试处理 iOS 上的推送通知。
我的简单代码看起来与此类似:
var Cloud = require("ti.cloud");
var deviceToken = null;
var deviceToken = Ti.App.Properties.getString('deviceToken');
Ti.App.iOS.registerUserNotificationSettings({
types: [
Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT,
Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND,
Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE
]
});
Ti.App.iOS.addEventListener('usernotificationsettings', function registerForPush() {
Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush);
Ti.Network.registerForPushNotifications({
success: function(e) {
if (e.deviceToken !== Ti.App.Properties.getString('deviceToken', null)) {
deviceToken = e.deviceToken;
Ti.App.Properties.setString('deviceToken', deviceToken)
subscribeToChannel();
} else {
Ti.API.info('Already registered for push notifications!');
}
},
error: function(e) {
Ti.API.error('Failed to register for push notifications: ' + e.error);
},
callback: receivePush
});
});
function subscribeToChannel () {
Cloud.PushNotifications.subscribeToken({
device_token: deviceToken,
channel: 'general',
type: Ti.Platform.name == 'android' ? 'android' : 'ios'
}, function (e) {
alert(e.success === true ? 'Subscribed' : 'Error!');
});
}
// When receieve interactive remote notification
Ti.App.iOS.addEventListener('remotenotificationaction', function(e) {
alert('remotenotificationaction: ' + JSON.stringify(e));
});
// When receieve interactive notification in the background
Ti.App.iOS.addEventListener('localnotificationaction', function(e) {
alert('localnotificationaction');
});
// When receieve interactive notification in the foreground
Ti.App.iOS.addEventListener('notification', function(e) {
alert('notification');
});
function receivePush(e) {
alert('receivePush');
}
在大多数情况下一切正常。当我发送远程推送通知时会发生以下情况:
- 当应用程序在后台运行时,会出现一条通知。单击通知后,我收到了预期的“receivePush”消息
- 当应用程序在前台时,不会出现通知,但我仍然会收到“receivePush”消息,这与预期一致。
- 但是,当我在应用程序处于后台时收到通知,然后直接点击应用程序(即不点击通知),以上事件都不会被触发!
我怎样才能确保为最后一个案例触发事件。
最佳答案
我认为这是不可能的,因为您的回调函数被分配了通知行为,而不是应用程序启动。如果您明白我的意思,这不是钛问题,而是工作流程误解。 我认为您最好在应用程序启动时始终检查一些与通知无关的内容。
关于ios - Titanium:在打开应用程序时检查 iOS 通知,而无需单击通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43782075/