我正在使用需要推送通知的appcelerator合金框架制作应用。我是第一次使用推送通知,所以请耐心帮助我。
我已经按照推送通知wiki教程https://wiki.appcelerator.org/display/guides2/Push+Notifications
进行了操作这是我的代码:
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() {
// Remove event listener once registered for push notifications
Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush);
Ti.Network.registerForPushNotifications({
success: deviceTokenSuccess,
error: deviceTokenError,
callback: receivePush
});
});
// Register notification types to use
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
});
}
// Process incoming push notifications
function receivePush(e) {
alert('Received push: ' + JSON.stringify(e));
}
// Save the device token for subsequent API calls
function deviceTokenSuccess(e) {
deviceToken = e.deviceToken;
}
function deviceTokenError(e) {
alert('Failed to register for push notifications! ' + e.error);
}
// Require the Cloud module
var Cloud = require("ti.cloud");
function subscribeToChannel () {
// Subscribes the device to the 'chats' channel
// Specify the push type as either 'android' for Android or 'ios' for iOS
Cloud.PushNotifications.subscribeToken({
device_token: deviceToken,
channel:'test',
type: Ti.Platform.name == 'android' ? 'android' : 'ios'
}, function (e) {
if (e.success) {
alert('Subscribed');
} else {
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
}
function unsubscribeToChannel () {
// Unsubscribes the device from the 'test' channel
Cloud.PushNotifications.unsubscribeToken({
device_token: deviceToken,
channel:'test',
}, function (e) {
if (e.success) {
alert('Unsubscribed');
} else {
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
}
function loginUser(username, password){
// Log in to Arrow
Cloud.Users.login({
login: username,
password: password
}, function (e) {
if (e.success) {
subscribeToChannel ();
alert('Login successful with device token' + deviceToken);
// Store the authentication details in the local filesystem
Ti.App.Properties.setString('usernameSave',username);
Ti.App.Properties.setString('passwordSave',password);
// user_id = jsonPost.SuccessResult.user_id;
} else {
alert('Error:\n' +
((e.error && e.message) || JSON.stringify(e)));
}
});
}
var savedUserName = Ti.App.Properties.getString('usernameSave','');
var savedPassword = Ti.App.Properties.getString('passwordSave','');
if(savedUserName != ''){
$.userNameField.value = savedUserName;
$.passwordField.value = savedPassword;
}
function login(){
var username = $.userNameField.value;
var password = $.passwordField.value;
loginUser(username, password);
}
单击名为login的按钮时,将调用Login()函数。
我在登录时获得了登录成功和订阅提醒。
每当我尝试向所有用户发送推送通知时,它都有效。但是,如果我尝试将其发送给指定的用户,则会在仪表板中的推送日志中出现故障。
我在这里缺少什么?请帮帮我。
感谢。
答案 0 :(得分:1)
好的,我发现了导致此问题的问题。
是的,这是我的错,因为在订阅方法中我使用令牌订阅而不是频道订阅。因为我正在使用基于会话的方法。
如果有人将来需要它,那就是区别。
检查第二行......
以前的代码
function subscribeToChannel () {
Cloud.PushNotifications.subscribeToken({
device_token: deviceToken,
channel:'test',
type: Ti.Platform.name == 'android' ? 'android' : 'ios'
}, function (e) {
if (e.success) {
alert('Subscribed');
} else {
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
}
新代码
function subscribeToChannel(){
Cloud.PushNotifications.subscribe({
device_token: deviceToken,
channel: 'test',
type: Ti.Platform.name == 'android' ? 'android' : 'ios'
}, function (e) {
if (e.success) {
alert('Subscribed');
} else {
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
}
谢谢。
干杯。