ios - Azure 移动服务 - 推送通知注册不起作用 [ios]

标签 ios azure push-notification azure-mobile-services azure-notificationhub

我正在构建一个 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 的屏幕截图中看到的 - 没有通知中心的注册。 Visual Studio Server Explorer 管理门户中监视器选项卡的输出位于此处。 Monitoring

我知道这是一个很长的问题,但如果您知道未到达推送通知的问题可能是什么 - 请告诉我! 谢谢

最佳答案

在 didRegisterForRemoteNotificationsWithDeviceToken 函数中,您将获得一个以“<”开头并以“>”结尾的 deviceToken。

你应该尝试删除这些字符,我很快就这样做了:

var deviceTokenString = "\(deviceToken)"
deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString("<", withString: "")
deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString(">", withString: "")

显然注册成功(这就是为什么 didFailToRegisterForRemoteNotificationsWithError 从未被调用),但它也失败了,很奇怪。

我建议您导航到移动服务、推送选项卡并单击通知中心。在服务总线通知中心,您有一个调试选项卡,可让您测试通知。每次您从那里发送通知时,您都可以看到包含注册的列表。

希望这有帮助。

关于ios - Azure 移动服务 - 推送通知注册不起作用 [ios],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31606732/

相关文章:

ios - NSDateFormatter,中间有自定义字符串

mysql - Azure 逻辑应用 - 用于 Azure 中托管的 Azure MySql 数据库的 MySql 连接器

azure - 以编程方式扩展 Windows Azure 角色

android - android 中的 gcm 注册失败

html - 如何在 Metro 应用程序上实现推送通知

ios 我可以更改默认密码屏幕行为或创建始终监听的按钮组合吗

ios - 在 iOS 中使用 Worklight Adapter API 启动 POST 请求类型

xcode - iOS远程配置?

azure - Azure 事件中心是否限制主题数量?

android - 推送到达时显示警报