2015-09-01 78 views
0

令牌ID我只是跟着的http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1教程創建樣品推送通知例子....不同的APN設備相同的應用程序

但我DONO我哪裏錯了,我在不同的接收不同的令牌ID用於相同應用的設備。

這裏是我的參考代碼,

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

     var type = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound; 
     var setting = UIUserNotificationSettings(forTypes: type, categories: nil); 
     UIApplication.sharedApplication().registerUserNotificationSettings(setting); 
     UIApplication.sharedApplication().registerForRemoteNotifications(); 

     return true 
    } 


    func application(application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 

     println("My token is \(deviceToken)") // am getting it different for different devices 
    } 

    //Called if unable to register for APNS. 
    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) { 

     println(error) 

    } 

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 

     println("Recived: \(userInfo)") 
     //Parsing userinfo: 
     var temp : NSDictionary = userInfo 
     if let info = userInfo["aps"] as? Dictionary<String, AnyObject> 
     { 
      var alertMsg = info["alert"] as! String 
      var alert: UIAlertView! 
      alert = UIAlertView(title: "", message: alertMsg, delegate: nil, cancelButtonTitle: "OK") 
      alert.show() 
     } 
    } 

還有一個問題是,我收到一個聲音和橫幅,但不會「徽章」

任何幫助,將不勝感激....

+0

你會得到不同的每個設備,這是預期的。 –

+0

哦!然後,如果我有一臺服務器,我不知道有多少人從應用程序商店下載了我的應用程序......在這種情況下,我將如何向所有人發送通知? –

+1

你必須在你的設計(服務器)中包含這個東西,你會發送用戶的設備ID到服務器,並根據需要發送推送 –

回答

1

在你的「didRegisterForRemoteNotificationsWithDeviceToken」 發送您的設備令牌服務器這樣

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
    let deviceTokenStr = HDDataLayer.convertDeviceTokenToStr(deviceToken) 
    HDDataLayer.postNotificationServiceResponseByUrlString("pushnotifications/notifications/register", andParams: tempDict, andDictCompletion: { (response: AnyObject!, error: NSError!) -> Void in 
     NSLog("Device Register successfully") 
    }) 
} 

請注意,HDDataLayer是我自己的類,您將使用您用來與您的服務器交互的類。

相關問題