ios - 我们如何在 iOS Appcelerator 中获取设备 token

标签 ios titanium appcelerator-studio

我正在使用 Appcelerator Studio。

我想从 iPhone 获取设备 token 。我正在关注 Appcelerator 文档,但是当应用程序安装时它会显示警告“你想要接收通知”,并且在点击它之后控制台中没有打印任何内容。

这是我的代码:

var self = Titanium.UI.createWindow({
    backgroundColor : '#146FA6',
    title : 'Menu',
});
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 : function(e) {
                var deviceToken = e.deviceToken;
                alert(e.deviceToken);
                Ti.API.info("Push notification device token is: " + deviceToken);
                Ti.API.info("Push notification types: " + Titanium.Network.remoteNotificationTypes);
                Ti.API.info("Push notification enabled: " + Titanium.Network.remoteNotificationsEnabled);
            },
            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 : function(e) {
            var deviceToken = e.deviceToken;
            alert(deviceToken);
            Ti.API.info("Push notification device token is: " + deviceToken);
            Ti.API.info("Push notification types: " + Titanium.Network.remoteNotificationTypes);
            Ti.API.info("Push notification enabled: " + Titanium.Network.remoteNotificationsEnabled);
        },
        error : deviceTokenError,
        callback : receivePush
    });
}
//deviceTokenSuccess();
// Process incoming push notifications
function receivePush(e) {
    alert('Received push: ' + JSON.stringify(e));
}

function deviceTokenError(e) {
    alert('Failed to register for push notifications! ' + e.error);
}

self.open();

最佳答案

如果启用了 LiveView,请将其禁用。

使用 LiveView 和推送通知服务时存在一些已知的冲突

要使用 Titanium Studio 禁用 LiveView 功能,请查看您的 Appcelerator Studio 工具栏,就像那样

enter image description here

并取消选择第一项。

关于ios - 我们如何在 iOS Appcelerator 中获取设备 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35146158/

相关文章:

ios - 使用自动布局在 super View 中水平居中 UILabel + UIImageView

ios - 本地化 iOS 应用程序时路径无效 - Xcode 7

iphone - 在 iPhone 应用程序中设置货币

javascript - 使用网络移动框架?

android - 应用程序无法在 Android 模拟器上运行

javascript - 如何获取Appcelerator应用程序中代码生成的元素

iphone - MKAnnotation - 多个注释未出现

model - 无法在 Titanium Appcelerator 中使用合金更新模型

android - 无法在 Windows : Cannot read property 'options' of undefined 上使用 5.3.0.GA 构建

css - 如何在appcelerator android中自定义选择器的大小?