gpt4 book ai didi

ios - Azure + Swift 推送通知

转载 作者:搜寻专家 更新时间:2023-10-31 08:18:59 27 4
gpt4 key购买 nike

我正在尝试从远程服务器到应用程序进行非常简单的推送。

我已按照 [1] 在 Azure 上设置了通知中心,但无法将调试消息发送到设备。 我不想使用移动服务从数据库表中读取/写入

我正在 Swift 中执行此操作,并且我在互联网上发现没有实际从服务器接收推送是 iOS swift 作为完整的教程。

我不知道,例如,如何在 swift 中编写以下代码:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// TODO: update @"MobileServiceUrl" and @"AppKey" placeholders
MSClient *client = [MSClient clientWithApplicationURLString:@"MobileServiceUrl" applicationKey:@"AppKey"];

[client.push registerNativeWithDeviceToken:deviceToken tags:@[@"uniqueTag"] completion:^(NSError *error) {
if (error != nil) {
NSLog(@"Error registering for notifications: %@", error);
}
}];
}

到目前为止,这是我在 AppDelegate 中的代码(我从 [2] 获得的一些代码):

    var client: MSClient?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:")))
{
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
}
/*else
{
//do ios7 stuff here. If you are using just local notifications then you dont need to do anything. for remote notifications:
application.registerForRemoteNotificationTypes(UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge)
}*/

self.client = MSClient(applicationURLString: "[url]", applicationKey: "[key]")

UIApplication.sharedApplication().registerForRemoteNotifications()
let settings = UIUserNotificationSettings(forTypes: .Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)

return true
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
println("Got device token \(deviceToken)");
//IS THIS EVEN CORRECT???? [3] and [4]
/*let json = ("{\"platform\":\"ios\", \"deviceToken\":\"\(deviceToken)\"}" as NSString).dataUsingEncoding(NSUTF8StringEncoding)

self.client?.invokeAPI("register_notifications", data: json, HTTPMethod: "POST", parameters: nil, headers: nil, completion:nil)*/
let registrationTags: [String] = ["tag"];
//EDIT 1 - I HAVE MADE A LITTLE PROGRESS
self.client?.push.registerNativeWithDeviceToken(deviceToken, tags: registrationTags, completion: { (error) -> Void in
println("Error registering")
})
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
println("Could not register \(error)");
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
println("Remote notification received!")
println("Awesome!")
}

我正在获取设备 token ,这意味着我应该注册,但我不知道如何正确实现[代码]

self.client?.push.registerNativeWithDeviceToken(deviceToken: deviceToken, tags: registrationTags, completion: [code])

编辑1我在这里取得了一些进展:

self.client?.push.registerNativeWithDeviceToken(deviceToken, tags: registrationTags, completion: { (error) -> Void in
println("Error registering")
})

现在我在注册时遇到错误:

Error Domain=com.Microsoft.WindowsAzureMobileServices.ErrorDomain Code=-1302 "Error: Internal Server Error" UserInfo=0x14d97b10 {NSLocalizedDescription=Error: Internal Server Error, com.Microsoft.WindowsAzureMobileServices.ErrorResponseKey= { URL: https://[servicename].azure-mobile.net/push/registrations%3FdeviceId=[longnumber]&platform=apns } { status code: 500, headers { "Cache-Control" = "no-cache"; "Content-Length" = 51; "Content-Type" = "application/json"; Date = "Thu, 05 Mar 2015 08:52:10 GMT"; Server = "Microsoft-IIS/8.0"; "Set-Cookie" = "ARRAffinity=[somehash];Path=/;Domain=[servicename].azure-mobile.net"; "X-Powered-By" = "ASP.NET"; "x-zumo-version" = "Zumo.master.0.1.6.4217.Runtime"; } }, com.Microsoft.WindowsAzureMobileServices.ErrorRequestKey= { URL: https://[servicename].azure-mobile.net/push/registrations%3FdeviceId=[longnumber]&platform=apns }}

编辑2

阅读[5]后,我现在做了以下更改:

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let token = NSString(data: deviceToken, encoding: NSUTF8StringEncoding)
println("Got device token");

let hub = SBNotificationHub(connectionString: CONNECTIONSTRING, notificationHubPath: HUBPATH)
hub.registerNativeWithDeviceToken(deviceToken, tags: nil, completion: {(error) -> Void in
println("Error registering: \(error)")
})
}

我现在看到的输出是:

Got device token
Error registering: nil

我觉得我正在取得进展,但是当我从 Azure 发送调试推送时,我在日志中看不到任何内容(目前,当我收到推送时,我只是打印一条消息)

引用文献:
[1]http://azure.microsoft.com/en-us/documentation/articles/notification-hubs-ios-get-started/
[2]Registering for iOS 7 notifications in swift
[3]https://github.com/Azure/azure-content/blob/master/articles/notification-hubs-ios-mobile-services-register-user-push-notifications.md
[4]http://azure.microsoft.com/en-us/documentation/articles/notification-hubs-ios-mobile-services-register-user-push-notifications/
[5]http://azure.microsoft.com/en-us/documentation/articles/notification-hubs-ios-get-started/

最佳答案

这对我来说效果很好:

let client: MSClient = MSClient(applicationURLString: "https://yoururl.azure-mobile.net/", applicationKey: "yourApplicationKey")
client.push.registerNativeWithDeviceToken(deviceToken, tags: nil, completion: {(error) -> Void in

if error != nil{
NSLog("Error registering for notifications: %@", error)
}

})

关于ios - Azure + Swift 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28872976/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com