Appcelerator Ti.Network.registerForPushNotifications 事件未在 iOS 上触发
Appcelerator Ti.Network.registerForPushNotifications events not firing on iOS
我需要为 iOS 设备实施 PushNotification。
订阅频道时的错误信息
缺少字段。必需:device_token 和频道
我有苹果证书和 Appcelerator 仪表板中的所有配置。
当我 运行 我设备上的应用要求确认推送通知时。
我认为错误的出现是因为 Ti.Network.registerForPushNotifications 事件未触发。
这是我的代码。
var Cloud = require("ti.cloud");
login();
function login() {
env = Ti.App.deployType.toLowerCase() === 'production' ? 'production' : 'development',
username = Ti.App.Properties.getString('acs-username-'+env),
password = Ti.App.Properties.getString('acs-password-'+env);
// Places your already created user id credential
Cloud.Users.login({
login : username,
password : password
}, function(e) {
if (e.success) {
var user = e.users[0];
Ti.API.info(' Login Success:\n' + 'id: ' + user.id + '\n' + 'sessionId: ' + Cloud.sessionId + '\n' + 'first name: ' + user.first_name + '\n' + 'last name: ' + user.last_name);
} else {
Ti.API.info('Error:\n' + ((e.error && e.message)));
}
});
}
var deviceToken = null;
// Check if the device is running iOS 8 or later
if (Ti.Platform.name == "iPhone OS" && 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() {
Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush);
Ti.Network.registerForPushNotifications({
success : deviceTokenSuccess,
error : deviceTokenError,
callback : receivePush
});
});
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]
});
}
// 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
});
}
function receivePush(e) {
//Ti.API.info('Received push: ' + JSON.stringify(e));
}
function deviceTokenSuccess(e) {
deviceToken = e.deviceToken;
Ti.API.info("eeeeeeee" + e);
//subscribeToChannel();
}
function deviceTokenError(e) {
Ti.API.info('ssssssssFailed to register for push notifications! ' + e.error);
}
function subscribeToChannel () {
// Subscribes the device to the 'news_alerts' channel
// Specify the push type as either 'android' for Android or 'ios' for iOS
Cloud.PushNotifications.subscribeToken({
device_token: deviceToken,
channel: 'news_alerts',
type: Ti.Platform.name == 'android' ? 'android' : 'ios'
}, function (e) {
if (e.success) {
Ti.API.info('Subscribed');
} else {
Ti.API.info('Error:\n' + ((e.error && e.message)));
}
});
}
function unsubscribeToChannel () {
// Unsubscribes the device from the 'test' channel
Cloud.PushNotifications.unsubscribeToken({
device_token: deviceToken,
channel: 'news_alerts',
}, function (e) {
if (e.success) {
Ti.API.info('Unsubscribed');
} else {
Ti.API.info('Error:\n' + ((e.error && e.message)));
}
});
}
setTimeout(function() {
subscribeToChannel();
}, 6000);
在调用 Arrow DB 之前,请确保您的 deviceTokenSuccessCB 已被触发,并且由于您已指示它未被触发,因此您可以按照以下步骤进行操作:
- 您的 iOS 设备设置已针对应用的通知开启。
- 您没有使用 LiveView ON 模式,因为这是这里的一个已知问题LiveView ON iOS Notification issue
我需要为 iOS 设备实施 PushNotification。
订阅频道时的错误信息 缺少字段。必需:device_token 和频道
我有苹果证书和 Appcelerator 仪表板中的所有配置。
当我 运行 我设备上的应用要求确认推送通知时。
我认为错误的出现是因为 Ti.Network.registerForPushNotifications 事件未触发。
这是我的代码。
var Cloud = require("ti.cloud");
login();
function login() {
env = Ti.App.deployType.toLowerCase() === 'production' ? 'production' : 'development',
username = Ti.App.Properties.getString('acs-username-'+env),
password = Ti.App.Properties.getString('acs-password-'+env);
// Places your already created user id credential
Cloud.Users.login({
login : username,
password : password
}, function(e) {
if (e.success) {
var user = e.users[0];
Ti.API.info(' Login Success:\n' + 'id: ' + user.id + '\n' + 'sessionId: ' + Cloud.sessionId + '\n' + 'first name: ' + user.first_name + '\n' + 'last name: ' + user.last_name);
} else {
Ti.API.info('Error:\n' + ((e.error && e.message)));
}
});
}
var deviceToken = null;
// Check if the device is running iOS 8 or later
if (Ti.Platform.name == "iPhone OS" && 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() {
Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush);
Ti.Network.registerForPushNotifications({
success : deviceTokenSuccess,
error : deviceTokenError,
callback : receivePush
});
});
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]
});
}
// 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
});
}
function receivePush(e) {
//Ti.API.info('Received push: ' + JSON.stringify(e));
}
function deviceTokenSuccess(e) {
deviceToken = e.deviceToken;
Ti.API.info("eeeeeeee" + e);
//subscribeToChannel();
}
function deviceTokenError(e) {
Ti.API.info('ssssssssFailed to register for push notifications! ' + e.error);
}
function subscribeToChannel () {
// Subscribes the device to the 'news_alerts' channel
// Specify the push type as either 'android' for Android or 'ios' for iOS
Cloud.PushNotifications.subscribeToken({
device_token: deviceToken,
channel: 'news_alerts',
type: Ti.Platform.name == 'android' ? 'android' : 'ios'
}, function (e) {
if (e.success) {
Ti.API.info('Subscribed');
} else {
Ti.API.info('Error:\n' + ((e.error && e.message)));
}
});
}
function unsubscribeToChannel () {
// Unsubscribes the device from the 'test' channel
Cloud.PushNotifications.unsubscribeToken({
device_token: deviceToken,
channel: 'news_alerts',
}, function (e) {
if (e.success) {
Ti.API.info('Unsubscribed');
} else {
Ti.API.info('Error:\n' + ((e.error && e.message)));
}
});
}
setTimeout(function() {
subscribeToChannel();
}, 6000);
在调用 Arrow DB 之前,请确保您的 deviceTokenSuccessCB 已被触发,并且由于您已指示它未被触发,因此您可以按照以下步骤进行操作:
- 您的 iOS 设备设置已针对应用的通知开启。
- 您没有使用 LiveView ON 模式,因为这是这里的一个已知问题LiveView ON iOS Notification issue