ios - Titanium Appcelerator 无法向指定用户发送推送通知

标签 ios push-notification titanium-alloy appcelerator-acs

我正在使用appcelerator合金框架制作一个需要推送通知的应用程序。我是第一次使用推送通知,所以请耐心等待并帮助我。

我已经按照此处的推送通知维基教程 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() 函数被调用。

我在登录时收到了登录成功已订阅警报。

每当我尝试向所有用户发送推送通知时,它都有效。但是,如果我尝试将它发送给指定的用户,它会在仪表板的推送日志上显示失败。

我在这里缺少什么?请帮帮我。

谢谢。

最佳答案

好的,我找到了导致这个问题的原因。

是的,这是我的错,因为在订阅方法中我使用的是 token 订阅而不是 channel 订阅。因为我正在使用基于 session 的方法。

这是区别,如果将来有人需要的话。

检查第二行...

上一个代码

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)));
        }
    });
}

谢谢。

干杯。

关于ios - Titanium Appcelerator 无法向指定用户发送推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34475650/

相关文章:

android - flex 移动项目中的多线程

ios - 尝试在 iOS 的 NSDateFormatter 对象中使用星期几的缩写

iOS 9 : Different push tokens returned for the same app (between installes)

ios - 角标(Badge)数量始终为1(云函数)

sqlite - 如何使用模型在钛中创建数据库表?

iOS:UIVIew 在 segue 和 popViewController 之后回到起始位置

ios - 如何防止 UITableViewCell 的背景 View 在点击时被清除?

javascript - 从 Web Angular 看 Notifications API 和 Push API 的区别

titanium - 在 Titanium JS 中,如何更改不活动的 Tab 色调颜色?

titanium - 如何处理在 Alloy xml 中动态创建 View