Push Notification delegate call backs are not gett

2019-02-24 02:14发布

问题:

I am registering for Push Notification by calling the following piece of code:

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

But in some rare cases following delegate does not get called:

- (void)application:(UIApplication *)iApplication didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)iNewDeviceToken {

Not even following method gets called:

- (void)application:(UIApplication *)iApplication didFailToRegisterForRemoteNotificationsWithError:(NSError *)iError {

What could be the reason for this. I am proceeding with my app flow once I received the device token. So, in some rare scenarios my app hungs.

Any clue?

回答1:

According to the documentation, neither of the callbacks will happen until the device has a persistent connection with the push server. So if there is no wifi or data connection available, the callbacks won't happen - and apple doesn't regard this as an error condition. As far as I can tell, the only errors that can happen to cause the didFail... callback are an incorrect certificate/app permissions issue (a development problem), or the user declined permission (though I am only sporadically able to reproduce this by changing the date and turning the phone off).



回答2:

The clue is right there: you get a NSError instance when it fails. So to figure out why it errors, inspect the NSError and see what its telling you.

You should probably at least handle that case in your app as well, such as showing an error message along the lines of "Failed to register application, please try again...".



回答3:

One more thing: If you phone is rooted/jail broken for use with another network, etc., you will have problems. My phone was rooted and none of the delegates were called. I had a coworker put it on their iPad, and it worked fine, so I know the code was OK.

So, I reinstalled the OS on my phone, and it started working.



回答4:

You have to enable the Push Notifications in the provisioning profile, set it up in the Developer Portal with the App Id. Then it should work.