ACS Appcelerator iOS未接收推送

时间:2016-01-16 19:52:24

标签: ios titanium push appcelerator acs

我目前正试图通过Appcelerator平台为dev / production环境进行推送通知。 我在apple开发者平台上创建了推送证书(dev / prod),我将其导出到.p12文件,并将其上传到我的ArrowDB iOS Push配置中。我的Titanium应用程序和我的ArrowDB似乎正确链接(生产和开发的好键)。

从我的应用程序中,我获得了设备令牌,我从订阅通知成功返回,我可以从Appcelerator看到我有一个ios设备已链接。

当我从Appcelerator ArrowDB平台发送推送通知时,我没有收到任何内容,而Appcelerator日志显示成功推送。

我的ACS通知处理代码:

var Cloud = require('ti.cloud');

var ANDROID = Ti.Platform.name === 'android';
var IOS = !ANDROID && (Ti.Platform.name === 'iPhone OS');
var BLACKBERRY = !ANDROID && !IOS && (Ti.Platform.name === 'blackberry');
Cloud.debug = true;  // optional; if you add this line, set it to false for production

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) {
    console.log('Received push: ' + JSON.stringify(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);
}



function ACSPush(acsuid, acspwd) {
    this.acsuid = acsuid || false;
    this.acspwd = acspwd || false;
}

ACSPush.prototype.registerDevice = function(channel_name, onReceive, onLaunched, onFocused, androidOptions, iosOptions, blackberryOptions) {
    var that = this,
        token = '';

    function deviceTokenSuccess(e) {
        console.log('Device Token: ' + e.deviceToken);
        token = e.deviceToken;
        that.token = token;
        loginToACS(that.acsuid, that.acspwd, token, channel_name);
    }

    function deviceTokenError(e) {
        console.log('Token Error: ' + e.error);
    }

    function receivePush(e) {
        onReceive(e.data);
        console.log("push notification received: " + JSON.stringify(e.data));
    }

    if (ANDROID) {
        var CloudPush = require('ti.cloudpush');
        CloudPush.retrieveDeviceToken({
            success : deviceTokenSuccess,
            error : deviceTokenError
        });

        CloudPush.focusAppOnPush = androidOptions.focusAppOnPush || false;
        CloudPush.showAppOnTrayClick = androidOptions.showAppOnTrayClick || false;
        CloudPush.showTrayNotification = androidOptions.showTrayNotification || false;
        CloudPush.showTrayNotificationsWhenFocused = androidOptions.showTrayNotificationsWhenFocused || false;
        CloudPush.singleCallback = androidOptions.singleCallback || true;
        CloudPush.addEventListener('callback', onReceive);
        CloudPush.addEventListener('trayClickLaunchedApp', onLaunched);
        CloudPush.addEventListener('trayClickFocusedApp', onFocused);

    } else if (IOS) {
        // Check if the device is running iOS 8 or later
        if (parseInt(Ti.Platform.version.split(".")[0]) >= 8) {
            function registerForPush() {
                Ti.Network.registerForPushNotifications({
                    success : deviceTokenSuccess,
                    error : deviceTokenError,
                    callback : receivePush
                });
                // Remove event listener once registered for push notifications
                Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush);
            };

            // Wait for user settings to be registered before registering for push notifications
            Ti.App.iOS.addEventListener('usernotificationsettings', registerForPush);

            // Register notification types to use
            Ti.App.iOS.registerUserNotificationSettings({
                types : iosOptions.types,
                categories : iosOptions.categories
            });

        } else {
            // For iOS 7 and earlier
            Ti.Network.registerForPushNotifications({
                // Specifies which notifications to receive
                types : iosOptions.types,
                success : deviceTokenSuccess,
                error : deviceTokenError,
                callback : receivePush
            });
        }

    } else if (BLACKBERRY) {
        Ti.BlackBerry.createPushService({
            appId : blackberryOptions.appId,
            ppgUrl : blackberryOptions.ppgUrl,
            usePublicPpg : blackberryOptions.usePublicPpg,
            launchApplicationOnPush : blackberryOptions.launchApplicationOnPush,
            onSessionCreated : function(e) {
                console.log('Session Created');
            },
            onChannelCreated : function(e) {
                console.log('Channel Created\nMessage: ' + e.message + '\nToken: ' + e.token);
                token = e.token;
                that.token = token;
                console.log("Device Token: " + token);
                loginToACS(that.acsuid, that.acspwd, token, channel_name);
            },
            onPushReceived : function(e) {
                onReceive(e.data);
                e.source.removeAllPushes();
            },
            onConfigError : function(e) {
                console.log('ERROR\nTitle: ' + e.errorTitle + +'\nMsg: ' + e.errorMessage);
            },
            onError : function(e) {
                console.log('ERROR\nTitle: ' + e.errorTitle + +'\nMsg: ' + e.errorMessage);
            },
            onAppOpened : function(e) {
                onLaunched(e.data);

                e.source.removePush(e.pushId);
            }
        });
    } else {
        alert("Push notification not implemented yet into acspushmod for " + Ti.Platform.osname);
    }
};

ACSPush.prototype.unsubscribeFromChannel = function(channel_name, token, onSuccess, onFail) {

    var that = this;
    Cloud.PushNotifications.unsubscribe({
        channel : channel_name,
        device_token : token
    }, function(e) {
        if (e.success) {
            onSuccess(e);
        } else {
            onFail(e);
        }
    });
};

ACSPush.prototype.getToken = function() {
    return this.token;
};
function loginToACS(acsuid, acspwd, token, channel_name) {
    if (!acsuid && !acspwd) {
        console.log("loginToACS -> subscribe as guest");
        subscribeForPushNotifications(token, channel_name, true);
        return;
    }
    Cloud.Users.login({
        login : acsuid,
        password : acspwd
    }, function(e) {
        if (e.success) {
            var user = e.users[0];
            console.log("loginToACS -> Status: Successful");
            subscribeForPushNotifications(token, channel_name);
        } else {
                console.log('acsuid = ' + acsuid + " acspwd = " + acspwd);
            console.log("loginToACS -> Error :" + e.message);
        }
    });
};

function subscribeForPushNotifications(token, channel_name, subscribeAsGuest) {
    var prams = {
        channel : channel_name,
        type : IOS ? 'ios' : Ti.Platform.osname, // osname return iphone / ipad on iOS
        device_token : token
    };
    var callBack = function(e) {
        if (e.success) {
            console.log('subscribeForPushNotifications -> Status: Successful [' + channel_name + ']');
        } else {
            console.log('subscribeForPushNotifications -> Error ' + token + '(subscribeToServerPush) :\\n' + ((e.error && e.message) || JSON.stringify(e)));
        }
    };
    if (subscribeAsGuest) {
        Cloud.PushNotifications.subscribeToken(prams, callBack);
    } else {
        Cloud.PushNotifications.subscribe(prams, callBack);
    }
};

此代码在我更改appcelerator帐户(迁移到我的客户帐户)之前曾经工作过。

如果你们知道我做错了什么,我将非常感激。

非常感谢!

环境:Appcelerator studio,Titanium SDK 5.0.0.GA,iphone5S

0 个答案:

没有答案