问题
I followed this thread, but the method didRegisterForRemoteNotificationsWithDeviceToken is still not called :
the documentation says :
After you call the registerForRemoteNotifications method of the UIApplication object, the app calls this method when device registration completes successfully
didRegisterUser appears well, but not did register notif.
Here is my code in the AppDelegate (the app version is 8.1) :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//register notif
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
return YES;
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
NSLog(@"didRegisterUser");
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"error here : %@", error);//not called
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/*
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
currentInstallation.channels = @[ @"global" ];
[currentInstallation saveInBackground];
*/
NSLog(@"did register notif");//not called
}
I also have background mode -> remote notification in the info.plist.
回答1:
Your code seems to be correct. As a minor improvement you could write your didRegisterUserNotificationSettings method like so:
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
if (notificationSettings.types != UIUserNotificationTypeNone) {
NSLog(@"didRegisterUser");
[application registerForRemoteNotifications];
}
}
There may be a configuration problem that causes a failed APN registration.
Assure that your Provisioning Profile contains the aps-environment entry
Assure that you have a unique App Identifier (a string without any "*") set in your Provisioning Profile. You should also use this exact identifier as "Bundle identifier" in your Info.plist
Maybe you have declined the Push-Feature after the initial installation - in this case you will never see the in-app-push-alert again and have to enable Push in the settings app again.
Try another device.
回答2:
After a long dig I found that on 19 July, 2016 due to some error or updation at Apple's end , the didRegisterForRemoteNotificationsWithDeviceToken method would not be called even if every condition like Internet connection , Device and the methods used are perfect.
Refer to this link for confirmation https://forums.developer.apple.com/thread/52224
To verify please have a look in your other apps too. I had wasted my several hours but hope it helps someone. Thanks.
回答3:
For 19 July, 2016:-
As per Apple Developer form, there is an issue regarding Sandbox APNS down... so there may be issue from apple side,thats why delegates like application:didRegisterForRemoteNotificationsWithDeviceToken: and application:didFailToRegisterForRemoteNotificationsWithError: is not called..
To check current status of APNS Sandbox this link... By now according to status APNS Sandbox is working fine and its normal ... so may be there is some other bug from apple side
So just don't worry if your methods are perfect and certificates are valid. This is just an issue from Apple side .. as soon as issue solved, your methods works perfectly(if everything is fine from your side).
Note that Production is working fine .. so issue is regarding only Sandbox APNS.
回答4:
I had this issue and finally found the note in Apple Developer web site and solved this issue:
Registering, Scheduling, and Handling User Notifications
iOS Note in the section: "Registering for Remote Notifications:
iOS Note: If a cellular or Wi-Fi connection is not available, neither the application:didRegisterForRemoteNotificationsWithDeviceToken: method nor the application:didFailToRegisterForRemoteNotificationsWithError: method is called. For Wi-Fi connections, this sometimes occurs when the device cannot connect with APNs over port 5223. If this happens, the user can move to another Wi-Fi network that isn’t blocking this port or, on an iPhone or iPad, wait until the cellular data service becomes available. In either case, the device should be able to make the connection, and then one of the delegation methods is called.
My iPhone only connected with Wifi, reboot iPhone and reconnect to WiFi AP solved this issue.
回答5:
XCode 8.3.1 onwards
After trying out all the above options, if the delegate method is still not called, make sure in the project,
under 'Capabilities', (next to General),
- Push Notifications option is turned ON`
- and under Background Modes, Remote Notifications is turned ON
回答6:
Shame on me!! O forgot the most basic step:
After following all the certificates process stuff instructions from here: https://www.pluralsight.com/guides/swift/creating-ios-rich-push-notifications
The tool for sending push notifications was: https://github.com/noodlewerk/NWPusher
May the Force be with you!
回答7:
Ok, guys, I've just spent 11 straight hours debugging this thing so hopefully, this answer will help somebody.
If you got EVERYTHING said in similar threads set up correctly and have got Google Firebase analytics integrated, and didRegisterForRemoteNotificationsWithDeviceToken method is still not called, this could be because it is swizzled in Google Firebase by default. For some weird reason, FIR doesn't return to the original method after it sends your token to Google. To fix it set
FirebaseAppDelegateProxyEnabled to NO (BOOL)
in your Info.plist.
回答8:
In my case, this was happening because I was proxying all device traffic through Charles. Removing that fixed it, and gave me the didRegister callback.
回答9:
In my case, after iOS 9.3 I believe. If I used Localytics, the didRegisterForRemoteNotificationsWithDeviceToken will not get called. I have to remove this from my code (in ApplicationDidFinishLaunchingWithOption) [Localytics autoIntegrate:@"xxxxx" launchOptions:launchOptions];
回答10:
For me, it was running the app on the simulator. Push token is not generated for simulators and so I was entering didFailToRegisterForRemoteNotificationsWithError instead of didRegisterForRemoteNotificationsWithDeviceToken
回答11:
Please add below code ,it working for me,
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
//handle the actions
if ([identifier isEqualToString:@"declineAction"]){
}
else if ([identifier isEqualToString:@"answerAction"]){
}
}
#endif
You will get device Token in below method ,
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
for detail please refer Details Answer
Hope this is help for some one .
回答12:
For IOS 13,
The didRegisterForRemoteNotificationsWithDeviceToken was not triggering at all when I connected to my wifi network. I tried a lot to fix this, but couldn't. At last I Changed my network from wifi to cellular data & suddenly it started working again. I even changed back to the old wifi network and it works with no issue.
来源:https://stackoverflow.com/questions/28128490/didregisterforremotenotificationswithdevicetoken-not-called-in-ios8-but-didregi