This question has been asked (e.g. here and here), but none of the solutions seems to apply to my setup.
I am developing an app in iOS 9 / Xcode7 (beta 5).
I have created an explicit App ID / bundle identifier.
(The app ID is automatically generated by Xcode, but is listen in the Developer Portal as Xcode iOS App ID com mycompanyname mayappname
).
I have configured my target's "Capabilities" in Xcode to add support for push notifications.
I have configured my App ID in the developer portal to support push notifications: I created and downloaded a development APNs certificate, and installed in my keychain (not sure this is needed? I thought this cert is intended for my server? But just to be safe in case it is needed at build time?)
I am running the app from Xcode on the device: iPhone 5s, iOS 9, 4G connectivity, Wi-Fi turned off (just in case).
My app is not listed in the device's Settings.app, "Notifications" section, so there's nothing I can enable there...
I initiate the registration process like this:
func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?
) -> Bool
{
application.registerForRemoteNotifications()
return true
}
I have implemented both:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
{
}
...and:
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError)
{
}
and setup breakpoints/logs on both, but neither gets called.
What can be happening?
EDIT: It is working now, but only if I call both registerForRemoteNotifications()
and registerUserNotificationSettings()
at launch.
I was working under the assumtion that I should first call registerForRemoteNotifications()
, and on success (i.e., application(_:didRegisterForRemoteNotificationsWithDeviceToken)
) I should in turn call registerUserNotificationSettings()
to specify the types of notifications (and prompt the user). The docs do not make clear which of these two functions must be called first; in particular, the inline docs for registerForRemoteNotifications()
say:
[...] Call this method to initiate the registration process[...]
, which seems to suggest that it alone triggers the whole thing. However, the programming guide has the following step-by-step instructions:
- Register the notification types your app supports using registerUserNotificationSettings:.
- Register to receive push notifications via APNs by calling your app’s registerForRemoteNotifications method.
- Store the device token returned to the app delegate by the server for a successful registration, or handle registration failure gracefully.
- Forward the device token to the app’s push provider.
I moved on to another project so I didn't follow up the issue or find a definitive response. Still listening to any helpful info...
Since this was originally answered,
registerUserNotificationSettings:
...has been deprecated, and,
requestAuthorizationWithOptions:completionHandler: // Objective C
...should now be used, or the Swift equivalent,
requestAuthorization(options:completionHandler:) // Swift
Here's an example of the Swift API in use,
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if let error = error {
// Handle the error here.
print(error.localizedDescription)
}
// Enable or disable features based on the authorization.
}
You were close.
You need first to call registerForRemoteNotificationTypes:
or registerUserNotificationSettings:
(iOS >=8)
And then call
registerForRemoteNotifications:
You have that example code:
// Register the supported interaction types.
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
// Register for remote notifications.
[[UIApplication sharedApplication] registerForRemoteNotifications];
Via: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With