我使用xcode和语言作为swift用于我的app.I&m。使用Azure进行推送通知。我创建了两个证书(Production / Sandbox)。当我使用沙箱证书通知时,我使用生产它不工作。 注意:在xcode 8中,我启用了自动管理签署配置文件
的功能有任何建议来解决这个问题吗?
我的代码:
// // AppDelegate.swift
import UIKit
import Alamofire
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var badgeCount:Int = 0
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//Push Notification Configs
NSLog("Push Notification Configured.")
let notiType:UIUserNotificationType = [.Alert ,.Badge , .Sound]
let settings = UIUserNotificationSettings(forTypes: notiType, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
//Push Notification Configs
let helper = PlistHelper()
let _webApiHelper = WebApiHelper()
let isFirstTimeUser = helper.getAnyBooleanValueFromPlistFile("IsFirstTimeUser")
if(isFirstTimeUser)
{
//Mark First Time Login
helper.addBooleanValueToPlistFile(false, Key: "IsFirstTimeUser")
//Show WelcomeController
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootController = storyboard.instantiateViewControllerWithIdentifier("welcomeScreenId") as! VideoPlayerViewController
if let window = self.window {
window.rootViewController = rootController
}
} else{
//If previously logged in
let url = _webApiHelper.BaseURL + "api/Account/ValidateUser"
let token = helper.getTOKENFromPlistFile()
let headers = [
"Authorization": "Bearer " + token,
"Accept": "application/json"
]
Alamofire.request(.POST, url, headers: headers).responseJSON
{ response in
if(response.response?.statusCode == 200)
{
//if IsLoggedIn -> go to home else go to login
let IsLoggedIn = helper.getAnyBooleanValueFromPlistFile("hasLoggedIn")
if(IsLoggedIn)
{
NSLog("Previously loggedIn - Go to home")
//Go to home
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootController = storyboard.instantiateViewControllerWithIdentifier("mainController") as! SWRevealViewController
if let window = self.window {
window.rootViewController = rootController
}
}else{
NSLog("Previously not loggoed in-Go to Login")
//Go to Login
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootController = storyboard.instantiateViewControllerWithIdentifier("ApploginControllerId") as! AppLoginViewController
if let window = self.window {
window.rootViewController = rootController
}
}
} else{
NSLog("Go to Login")
//Go to Login
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootController = storyboard.instantiateViewControllerWithIdentifier("ApploginControllerId") as! AppLoginViewController
if let window = self.window {
window.rootViewController = rootController
}
}
}//END - request
}
return true
}
//Added for APNS
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Failed to register for remote notifications: ", error.description)
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
{
IsValidUser(){ (boolValue) -> () in
//If registerd user register for push notifications
if(boolValue)
{
NSLog("Registering for push")
let hub = SBNotificationHub(connectionString:HUBLISTENACCESS, notificationHubPath:HUBNAME)
hub.registerNativeWithDeviceToken(deviceToken, tags: nil, completion: { (error) in
print("Device Token \(deviceToken) \n" )
if (error != nil)
{
NSLog("Error registering for notification: \(error)")
}
})
}
}
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo:[NSObject : AnyObject]) {
let dics = userInfo as NSDictionary
print("Recived: \(userInfo)")
print("Msg:: \(dics.objectForKey("aps")?.objectForKey("alert"))")
print("Test:: \(dics.objectForKey("customPayload"))")
var msgTitle = ""
var message = ""
//Message Title
if let Title = (dics.objectForKey("customPayload")!.objectForKey("Title"))
{
msgTitle = Title as! String
} else
{
NSLog("Alert message is Null")
}
//Push Message
if let msg = dics.objectForKey("customPayload")!.objectForKey("Message")
{
message = msg as! String
} else
{
NSLog("Alert message is Null")
}
NSNotificationCenter.defaultCenter().postNotificationName("ReceivedNotification", object:userInfo)
if application.applicationState == .Active
{
MessageBox(msgTitle, message: message as NSString)
}else{
//Go to Wall post on notification click.
let id = dics.objectForKey("customPayload")!.objectForKey("Id") as! Int
ViewPostInDetailView(id)
}
}
func ViewPostInDetailView(id:Int) -> Void
{
let helper = PlistHelper()
let IsLoggedIn = helper.getAnyBooleanValueFromPlistFile("hasLoggedIn")
if(IsLoggedIn)
{
//Goto Detail post
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootController = storyboard.instantiateViewControllerWithIdentifier("PostDetail4STBID") as! PostDetailViewController4
if let window = self.window {
rootController.PostId = id
rootController.FromNotification = true
window.rootViewController = rootController
}
}else{
NSLog("Go to Login - user is not logged in.")
//Go to Login
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootController = storyboard.instantiateViewControllerWithIdentifier("ApploginControllerId") as! AppLoginViewController
if let window = self.window {
window.rootViewController = rootController
}
}
}
func MessageBox(title:NSString,message:NSString){
let alertController = UIAlertController(title: title as String , message: message as String, preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(defaultAction)
self.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
//UIApplication.sharedApplication().applicationIconBadgeNumber = 0
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
NSLog("applicationDidEnterBackground")
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
NSLog("applicationWillEnterForeground")
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
// UIApplication.sharedApplication().applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
//badgeCount = application.applicationIconBadgeNumber
//badgeCount = badgeCount + 1;
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
//check is valid user by token
func IsValidUser( completion: (result: Bool)->()) {
let _webHelper = WebApiHelper()
let url_ValidateUser = _webHelper.BaseURL + "api/Account/ValidateUser"
let _plist = PlistHelper()
let token = _plist.getTOKENFromPlistFile()
let headers = [
"Authorization": "Bearer " + token,
"Accept": "application/json"
]
Alamofire.request(.POST, url_ValidateUser, headers: headers).responseJSON
{ response in
if(response.response?.statusCode == 200)
{
completion(result: true)
} else{
completion(result: false)
}
}//END - request
}
}