我正在构建一个 iOS 应用程序,该应用程序连接到 Azure 移动服务后端以发送推送通知 - 每次在表中插入新项目时。注册似乎不起作用,因为在通知中心的仪表板中我可以看到一些“注册操作”,但我在 Visual Studio Server Explorer 中看不到任何注册,而且推送通知也没有到达设备上。
我使用此代码在我的 iOS 客户端上注册远程通知。这段代码运行良好,没有出现任何错误。
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[self.client.push registerNativeWithDeviceToken:deviceToken tags:nil completion:^(NSError *error) {
if (error != nil) {
NSLog(@"Error registering for notifications: %@", error);
}
}];
}
执行此代码是为了在移动服务的表存储中插入一个新项目。
NSDictionary *dbitem = @{ @"message" : @"test"};
MSTable *itemTable = [client tableWithName:@"TestTable"];
[itemTable insert:dbitem completion:^(NSDictionary *insertedItem, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"Item inserted, id: %@", [insertedItem objectForKey:@"id"]);
}
}];
插入工作正常,项目显示在表存储中。每次在表中插入新项目时,此脚本都会运行(Javascript - Node.js 后端)
function insert(item, user, request) {
var userId = user.userId;
request.execute({
success: function() {
request.respond();
push.apns.send(userId, {
alert: "Hello!",
payload: {
inAppMessage: item.message
}
});
}
});
}
最后,这是我用来注册新用户以接收推送通知的注册脚本。
exports.register = function (registration, registrationContext, done) {
// Get the ID of the logged-in user.
var userId = registrationContext.user.userId;
console.log("userid: %j", userId);
// Perform a check here for any disallowed tags.
if (!validateTags(registration))
{
// Return a service error when the client tries
// to set a user ID tag, which is not allowed.
done("You cannot supply a tag that is a user ID");
console.log("failed registraion");
}
else{
// Add a new tag that is the user ID.
registration.tags.push(userId);
console.log("done registraion");
// Complete the callback as normal.
done();
}
};
function validateTags(registration){
for(var i = 0; i < registration.tags.length; i++) {
console.log(registration.tags[i]);
if (registration.tags[i]
.search(/facebook:|twitter:|google:|microsoft:/i) !== -1){
return false;
}
return true;
}
}
正如您在 Visual Studio Server Explorer 的屏幕截图中看到的 - 没有通知中心的注册。 管理门户中监视器选项卡的输出位于此处。
我知道这是一个很长的问题,但如果您知道未到达推送通知的问题可能是什么 - 请告诉我! 谢谢
最佳答案
在 didRegisterForRemoteNotificationsWithDeviceToken 函数中,您将获得一个以“<”开头并以“>”结尾的 deviceToken。
你应该尝试删除这些字符,我很快就这样做了:
var deviceTokenString = "\(deviceToken)"
deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString("<", withString: "")
deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString(">", withString: "")
显然注册成功(这就是为什么 didFailToRegisterForRemoteNotificationsWithError 从未被调用),但它也失败了,很奇怪。
我建议您导航到移动服务、推送选项卡并单击通知中心。在服务总线通知中心,您有一个调试选项卡,可让您测试通知。每次您从那里发送通知时,您都可以看到包含注册的列表。
希望这有帮助。
关于ios - Azure 移动服务 - 推送通知注册不起作用 [ios],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31606732/