알림을 전달하려면 해당 주소 인 서버에 알림 accessToken을 보내야합니다. accesstoken의 변형에 대해 걱정할 필요가 없습니다. 매번 로그인 할 때마다 보내야하기 때문에 새로운 업데이트 된 accesstoken도 서버에 추가됩니다. Appdelegate에 원격 알림을 등록하고 나중에 저장된 토큰을 보내야합니다. 로그인 API에서 nsuserdefault에서 서버로.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
return true
}
//Called if successfully registered for APNS.
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
// let deviceTokenString = NSString(format: "%@", deviceToken) as String
var tokenStr = deviceToken.description
tokenStr = tokenStr.stringByReplacingOccurrencesOfString("<", withString: "", options: [], range: nil)
tokenStr = tokenStr.stringByReplacingOccurrencesOfString(">", withString: "", options: [], range: nil)
tokenStr = tokenStr.stringByReplacingOccurrencesOfString(" ", withString: "", options: [], range: nil)
print(deviceToken.description)
print(tokenStr)
//save the token in NSUserDefaults
NSUserDefaults.standardUserDefaults().setObject(deviceTokenString, forKey: "deviceToken")
}
//Called if unable to register for APNS.
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print(error)
}
Reference Apple's Documentation
장치 토큰은 특정 장치에 응용 프로그램 에 푸시 알림을 전송하는 키입니다. 기기 토큰이 변경 될 수 있으므로 앱을 시작할 때마다 을 다시 등록해야하고 수신 된 토큰을 서버에 다시 전달하십시오 ( ). 기기 토큰을 업데이트하지 못하면 원격 알림이 사용자 기기로 전송되지 않을 수 있습니다. 장치 토큰은 사용자가 백업 데이터를 새로운 장치 또는 컴퓨터로 복원하거나 운영 체제를 다시 설치하면 항상 변경됩니다. 데이터를 새 기기 또는 컴퓨터로 이전 할 때 사용자는 번 앱을 실행해야 원격 알림을 해당 기기로 전송할 수 있습니다.
이 답변을 주셔서 감사합니다.하지만 두 사람 사이에 알림을 보내고 싶다면 여전히 데이터베이스에 저장합니까? 고마워요 –
예, 사용자가 9 명 이후로 앱을 재설치하거나 휴대 전화를 복원 할 때 액세스 권한이 변경 되어도 사람이 여러 명일 수있는 경우에도 두 사람 사이의 데이터베이스가 변경 되더라도 데이터베이스를 유지 관리해야합니다. 따라서 데이터베이스를 반드시 유지 관리해야합니다. –