2014-03-27 140 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