我知道 this问题。但我认为这个问题仍然存在。所以我的要求是不要将这个问题标记为重复。
当 iOS 应用程序处于后台或未运行时,设备会收到通知。但是当应用程序处于前台时,通知不会显示。我正在使用文档中提到的相同代码 here
操作系统:iOS 11.2.5, 手机:iPhone 6 钛SDK:7.0.2.GA
下面是我的代码供引用。
exports.createNotification = function() {
if (Ti.UI.iOS.appBadge !== 0) {
Ti.UI.iOS.appBadge = 0;
}
var deviceToken = null;
// Check if the device is running iOS 8 or later
if (Ti.Platform.name == "iOS" && parseInt(Ti.Platform.version.split(".")[0]) >= 8) {
// Wait for user settings to be registered before registering for push notifications
Ti.App.iOS.addEventListener('usernotificationsettings', function registerForPush() {
// Remove event listener once registered for push notifications
Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush);
Ti.Network.registerUserNotificationSettings({
success : deviceTokenSuccess,
error : deviceTokenError,
callback : receivePush
});
});
// Register notification types to use
Ti.App.iOS.registerUserNotificationSettings({
types : [Ti.Network.NOTIFICATION_TYPE_BADGE, Ti.Network.NOTIFICATION_TYPE_ALERT, Ti.Network.NOTIFICATION_TYPE_SOUND]
});
}
// For iOS 7 and earlier
else {
Ti.Network.registerForPushNotifications({
// Specifies which notifications to receive
types : [Ti.Network.NOTIFICATION_TYPE_BADGE, Ti.Network.NOTIFICATION_TYPE_ALERT, Ti.Network.NOTIFICATION_TYPE_SOUND],
success : deviceTokenSuccess,
error : deviceTokenError,
callback : receivePush
});
}
// Process incoming push notifications
function receivePush(e) {
Ti.API.info('Received push: ' + JSON.stringify(e));
//alert('Received push: ' + JSON.stringify(e));
if (e.data !== null) {
//exports.insertPushToDb(e.data);
}
}
// Save the device token for subsequent API calls
function deviceTokenSuccess(e) {
deviceToken = e.deviceToken;
Ti.App.Properties.setString('PushNotificationRegID', deviceToken);
Ti.API.info('Push notification: ' + deviceToken);
//alert('deviceToken ' + deviceToken);
}
function deviceTokenError(e) {
Ti.API.info('Error Noti: '+JSON.stringify(e));
//alert('Failed to register for push notifications! ' + e.error);
}
};
提前致谢。
最佳答案
您将显示推送的行注释掉了!
//alert('Received push: ' + JSON.stringify(e));
重新启用这条线,你会再次看到它。
关于ios - Appcelerator iOS 推送通知在应用程序处于前台时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48943311/