2014-03-27 139 views
5

我知道,你可以得到令牌的設備與方法: -在設備註冊推送通知後,iOS獲取設備令牌?

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 

但是,如果我理解正確此方法只被稱爲第一次應用程序運行和用戶接受的通知要求。在隨後重新啓動應用程序時,該方法再也不會被調用。

所以我的問題是這樣的 - 有沒有其他的方式來訪問設備令牌後,用戶已經接受通知?

編輯 - 我知道設備處於在線狀態,並且臨時配置文件與應用ID相關聯,因爲我可以接收推送通知。接收推送通知不是問題 - 獲取設備令牌並將其存儲在Parse中是我想實現的。

感謝

+1

也許' - (void)應用程序:(UIApplication *)應用程序didFailToRegisterForRemoteNotificationsWithError:(NSError *)error'被調用。如果是這樣,你可以看看這個錯誤,這會讓你知道發生了什麼問題。 – xardas

+1

感謝xardas確實幫助錯誤處理程序調用 - 看起來這是一個配置文件問題 - 請參閱下面的Smikey答案。但奇怪的是,它仍在受到推動。 – BlinkingCahill

回答

1

嗯,再查看您的provisioning profile的應用程序ID的配置輪廓的建築。這聽起來像你可能正在使用通配符。您可能正在接收來自服務器的推送,因爲您最初使用正確的配置文件進行了部署,但是如果您更改爲使用通配符配置文件(例如團隊配置文件),則不會正確註冊遠程通知...

0

使用以下方法didiFinishLaunchingWithOption

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; 

可能這會爲你的問題

+1

問題是,一旦registerForRemoteNotificationTypes執行一次,它將不會再次調用didRegisterForRemoteNotificationsWithDeviceToken。即使你刪除了應用程序,調用unRegister等,該應用程序不會再次調用didRegisterForRemote ...。 – Smikey

9

你錯了工作。

-application:didRegisterForRemoteNotificationsWithDeviceToken:文檔:

The delegate receives this message after the registerForRemoteNotificationTypes: method of UIApplication is invoked and there is no error in the registration process [...] This method could be called in other rare circumstances, such as when the user launches an app after having restored a device from data that is not the device’s backup data. In this exceptional case, the app won’t know the new device’s token until the user launches it.

從蘋果推送通知指南:

Moreover, never cache a device token and give that to your provider; always get the token from the system whenever you need it. If your application has previously registered, calling registerForRemoteNotificationTypes: results in the operating system passing the device token to the delegate immediately without incurring additional overhead. Also note that the delegate method may be called any time the device token changes, not just in response to your app registering or re-registering.

在每當調用registerForRemoteNotificationTypes:和操作成功,以及該方法被調用換句話說在令牌更改的其他時間

編輯:

If a cellular or Wi-Fi connection is not available, neither the application:didRegisterForRemoteNotificationsWithDeviceToken: method or the application:didFailToRegisterForRemoteNotificationsWithError: method is called. For Wi-Fi connections, this sometimes occurs when the device cannot connect with APNs over port 5223.

或者確保您有鏈接到具有推送通知的權利

+0

好吧,或許我仍然不明白我打電話給'registerForRemoteNotificationTypes',我已經把日誌語句和一個斷點放在'didRegisterForRemoteNotificationsWithDeviceToken'中 - 但是我沒有觸及斷點或者獲取那些日誌消息 - 還有別的東西我做錯了? – BlinkingCahill

+0

我知道設備在線並且臨時配置文件鏈接到應用ID,因爲我可以接收推送通知。讓我清楚接收推送通知不是問題 - 獲取設備令牌並將其存儲在Parse中是我想實現的。 – BlinkingCahill

+0

你有沒有解決這個問題?我遇到類似的問題 – John