我本月开始使用Azure App Service,在App Service I使用Azure移动服务之前,带有标签的注册设备令牌非常简单但在应用服务中我意识到由于安全问题而将其删除所以我必须这样做使用自定义API。
我使用自定义身份验证(不是天蓝色身份验证服务(由于我的客户端不希望它))和我的数据库,所以我需要将用户ID设置为标签,以便向特定用户发送通知。但是我面对的是问题,即使设备令牌注册正常(我可以发送推送给没有标签的每个人)标签不工作,我正在关注这些博客帖子
我的自定义API(updateNotification.js)
var api = {
get: (request,response,next) => {
var push = request.azureMobile.push;
var installationId = request.query.id;
push.getInstallation(installationId, function(error, installation, res){
if (error){
console.log('An error occurred when retrieving installation : ' + error);
response.status(error.statusCode).send(error.detail);
}
else{
// Return an array of current tags.
response.json(installation.tags);
}
});
},
post: (request, response, next) => {
var push = request.azureMobile.push;
var installationId = request.query.id;
var tags = request.query.userID;
var updateOperation = [{
"op": "add",
"path": "/tags",
"value": tags.toString()
}];
push.patchInstallation(installationId, updateOperation, function(error, res){
if(error){
console.log(error)
response.status(error.statusCode).send(error.detail);
}
else{
console.log("Success");
console.log("Tags : " + tags);
response.status(200).send(tags);
}
});
}
};
module.exports = api;
在我的AppDelegate.swift类中,我正在做这个
func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let keychain = KeychainSwift()
let id : NSString = keychain.get("userID")! as String
let client = MSClient(applicationURLString: "https://XXXX.XXXX.XXX")
var deviceTokenString = "\(deviceToken)"
deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString("<", withString: "")
deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString(">", withString: "")
deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString(" ", withString: "-")
client.push?.registerDeviceToken(deviceToken, completion: { (error) in
if let err = error {
print("ERROR ", err)
}else{
client.invokeAPI("updateNotification", body: nil, HTTPMethod: "Post", parameters: ["id" : deviceTokenString , "userID" : id], headers: nil) { (result, response, error) in
if response?.statusCode != 200 {
NSLog("ERROR %@", error!)
} else {
print("Tags Successfully Implemented")
}
}
}
})
}
现在一切似乎都很好,在我的控制台中我可以看到我的用户ID,我的设备令牌和用户ID在这里就像这样(我在它的中间放置了X:对不起)
deviceToken = 22afedf6-a08f1ce9-XXXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXX-db431577-2dfbbee1 用户ID = 88d06c97-XXXX-XXXX-XXXX-042215c46575
但是,当我尝试使用此GET方法查看设备ID的标签时,
client.invokeAPI("updateNotification", body: nil, HTTPMethod: "GET", parameters: ["id" : deviceTokenString], headers: nil) { (result, response, error) in
if response?.statusCode != 200 {
NSLog("ERROR %@", error!)
} else {
print(result)
}
}
我收到此错误:
错误Domain = com.Microsoft.MicrosoftAzureMobile.ErrorDomain Code = -1302 “没有安装 found.TrackingId:57239dd3-XXXX-XXXX-XXXX-0bd9579c660e_G1,时间戳:2016年7月18日 晚上8:22:05“
如何解决此错误消息?
答案 0 :(得分:4)
好的,经过长时间工作,这里就是解决方案
<强> 1.Step 强>
如果你可以debug the Notification Hub(我建议从Visual Studio 2015 Express等调试......),你可以看到PNS标识符(哪个是我们的设备令牌)都是资本没有破折号,所以首先我们需要更改我们的AppDelegate.swift中的设备令牌代码,这将是这样的
deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString("<", withString: "")
deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString(">", withString: "")
deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString(" ",withString: "-")
deviceTokenString = deviceTokenString.uppercaseString
//In case of any spaces just guarantee with trim
let trimmedDeviceToken = deviceTokenString.stringByTrimmingCharactersInSet(
NSCharacterSet.whitespaceAndNewLineCharacterSet()
)
<强> 2.Step 强>
InstallationId和Device令牌完全不同。你要将标签推送到installationId而不是设备令牌,这样你就需要在客户端代码中获得installationId。
问题出现在Azure Mobile SDK for IOS中,您无法使用client.installationId
调用安装ID(至少我找不到从MSClient对象获取的方式)
我做的是,进入框架并找到MSClient.m。我意识到安装ID实际上存储在NSUserDefaults中,密钥为“WindowsAzureMobileServicesInstallationId”。所以我可以从NSUser到达它默认使用此
let defaults = NSUserDefaults.standartUserDefaults()
let installationID = defaults.stringForKey("WindowsAzureMobileServicesInstallationId")
执行这些步骤之后,您可以实际将标记注册到安装ID。这是结果和应用程序的完整代码didRegisterForRemoteNotificationsWithDeviceToken方法
func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let keychain = KeychainSwift()
let id : NSString = keychain.get("userID")! as String
let client = MSClient(applicationURLString: "https://XXXX.XXXX.XXX")
var deviceTokenString = "\(deviceToken)"
deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString("<", withString: "")
deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString(">", withString: "")
deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString(" ", withString: "")
deviceTokenString = deviceTokenString.uppercaseString
//In case of any spaces just guarantee with trim
let trimmedDeviceToken = deviceTokenString.stringByTrimmingCharactersInSet(
NSCharacterSet.whitespaceAndNewLineCharacterSet()
)
let defaults = NSUserDefaults.standartUserDefaults()
let installationID = defaults.stringForKey("WindowsAzureMobileServicesInstallationId")
client.push?.registerDeviceToken(deviceToken, completion: { (error) in
if let err = error {
print("ERROR ", err)
}else{
client.invokeAPI("updateNotification", body: nil, HTTPMethod: "Post", parameters: ["id" : installationID! , "userID" : id], headers: nil) { (result, response, error) in
if response?.statusCode != 200 {
NSLog("ERROR %@", error!)
} else {
print("Tags Successfully Implemented")
}
}
}
})
}
在此之后,您还可以通过调用我们从NSUserDefaults获取的installationId调用updateNotification的GET方法来检查标记