Appcelerator iOS 当应用程序处于前台时推送通知不工作
Appcelerator iOS Push notification not working when app is in Foreground
我知道 问题。但我认为这个问题仍然存在。所以我的要求是不要将这个问题标记为重复。
当 iOS 应用程序是否处于后台时 运行 设备会收到通知。但是当应用程序处于前台时,通知不会显示。我使用的是文档 here
中提到的相同代码
OS: iOS 11.2.5,
Phone: 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));
Re-enable这一行,你会再次看到它。
我知道
当 iOS 应用程序是否处于后台时 运行 设备会收到通知。但是当应用程序处于前台时,通知不会显示。我使用的是文档 here
中提到的相同代码OS: iOS 11.2.5, Phone: 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));
Re-enable这一行,你会再次看到它。