2016-02-02 68 views
2

我正在使用Appcelerator Studio。我想從iPhone獲取device token。我正在關注Appcelerator文檔,但是當安裝它的應用程序向我顯示「您是否希望收到通知」警報時,點擊它之後,控制檯中將不會顯示任何內容。我們如何在iOS 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(); 
+0

是否使用模擬器或者設備? –

+0

我正在使用device.iphone 5s操作系統(9.2.1) – SAK

回答

1

如果啓用了實時查看,禁用它。

有一些已知的衝突,使用實時視圖和推送通知服務

時要使用鈦工作室,看看你的Appcelerator的工作室工具欄禁用實時查看功能,這樣

enter image description here

並取消第一項。

+0

我該如何禁用ListView – SAK

+0

非常感謝親愛的......它現在可以..... :) – SAK

1

的代碼應該是這樣的

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

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

謝謝親愛的...... :) – SAK