I'm currently trying to implementation Auth for iOS using Firebase. I've gotten it to work using the recaptcha method specified here: https://firebase.google.com/docs/auth/ios/phone-auth but still running into issues with the silent APN. I uploaded my APN key to the firebase project and added push notification to my swift project capabilities. I also added the following piece of code to my AppNameApp.swift file:
class Appdelegate : NSObject,UIApplicationDelegate{
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
UIApplication.shared.registerForRemoteNotifications()
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
}
}
I'm still unable to get it to work and I've been stuck for a few days now. Any help will be greatly appreciated!
Related
I have the Firebase SDK in my project for the backend and I am using the Beams SDK from Pusher to configure push notifications.
The issue is that, it doesn't seem that my app delegate is configuring the settings I need to send notification with the Beams SDK if I have Firebase. Here is what I mean.
According to the Beams SDK I need to place this code in the didFinishLaunchingWithOptions
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
self.pushNotifications.start(instanceId: "MyInstanceID")
self.pushNotifications.registerForRemoteNotifications()
try? self.pushNotifications.addDeviceInterest(interest: "debug-hello")
// FirebaseApp.configure() . However it ONLY works properly when this is commented out
return true
}
As I showed above, it only configures my notifications properly if I comment out the Firebase configuration. Here is other relevant code in my delegate:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
print("RECEIVED NOTIFICATION")
self.pushNotifications.handleNotification(userInfo: userInfo)
if Auth.auth().canHandleNotification(userInfo){
completionHandler(.noData)
return
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("Did register for remote notifications")
self.pushNotifications.registerDeviceToken(deviceToken)
Auth.auth().setAPNSToken(deviceToken, type: .prod)
}
How do I properly configure both in my project?
The issue is that Firebase.configure() will break the didRegisterForRemoteNotificationsWithDeviceToken method.
You need to add GoogleUtilitiesAppDelegateProxyEnabled to the info.plist and set it to NO.
I have been using urban airship in my iOS app and my backend for push notifications. Recently, I had to migrate my phone number authentication from digits to firebase which involved using Firebase Cloud messaging. Firebase Cloud messaging does not work until I comment out the setup of urban airship. Is there a way to use both? What might be causing the problem? I don't want to change my current push notification setup for this migration.
My AppDelegate currently looks like this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIBarButtonItem.appearance()
.setTitleTextAttributes([NSFontAttributeName : UIUtils.getFont(14)],
for: UIControlState())
FirebaseApp.configure()
// setupUrbanAirship()
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Pass device token to auth
Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.sandbox)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
Auth.auth().canHandleNotification(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
if Auth.auth().canHandleNotification(userInfo) {
completionHandler(.noData)
return
}
}
fileprivate func setupUrbanAirship() {
let filename = PropertyUtils.getUAPropertyFileName()
let path = Bundle.main.path(forResource: filename, ofType: "plist")
let config = UAConfig(contentsOfFile: path!)
//config.automaticSetupEnabled = false
UAirship.takeOff(config)
let channelID = UAirship.push().channelID
print("My Application Channel ID: \(channelID)")
UAirship.push().userPushNotificationsEnabled = true
}
Just had to use manual integration steps of urban airship and got their latest build updated in my app.
I have set up the silent push notification for my app:
1. I configured the push notification from all places, i.e., XCode, Apple Developer portal with proper certificate
2. I enabled background capability
3. I included "content-available" in the json payload.
However, my App can not receive silent push sometimes.
To be more specific, neither
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
......
}
nor
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: #escaping (_: UIBackgroundFetchResult) -> Void) {
......
}
has been invoked when the push message arrives.
How can I get the information in the push notification in such state?
I'm waiting online.
You question is twofold:
1. why the two delegate callbacks can not be invoked
2. what should you do to receive the data.
I did some hand test and here is the result:
1, when an app is in killed state, the two callback can not be invoked indeed
2. however, when you open the app next time,
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
......
}
can be invoked and you can get the data from there.
I hope this is useful.
I am integrating Authenticate with Firebase on iOS using a Phone Number and I am having errors in the initial implementation of the procedure as mentioned in their document in this link.
I followed the procedure mentioned their, installed the pods and imported the firebase SDK.
The error comes in the following two methods:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Pass device token to auth
Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenTypeProd)
// Further handling of the device token if needed by the app
// ...
}
and
func application(_ application: UIApplication,
didReceiveRemoteNotification notification: [AnyHashable : Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
if Auth.auth().canHandleNotification(notification) {
completionHandler(UIBackgroundFetchResultNoData)
return
}
// This notification is not auth related, developer should handle it.
}
It says "use of unresolved identifier 'AuthAPNSTokenTypeProd'" and "use of unresolved identifier 'UIBackgroundFetchResultNoData'".
I am not sure about the procedure I am missing in the implementation. I have gone through the implementation procedure many times, I can't find the mistake.
Please try following code
Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.prod)
and this
func application(_ application: UIApplication,
didReceiveRemoteNotification notification: [AnyHashable : Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
if Auth.auth().canHandleNotification(notification) {
completionHandler(UIBackgroundFetchResult.noData)
return
}
// This notification is not auth related, developer should handle it.
}
It turns out AuthAPNSTokenTypeProdis not a data type or enum case. It seems like there's a mistake in the Phone Authentication implementation steps for iOS.
You're looking for AuthAPNSTokenType which is associated with the Obj-C enum: FIRAuthAPNSTokenType.
You can find the source inside the FirebaseAuth framework/dependency.
So the solution to one of your problems should be to change this line:
Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenTypeProd)
to this:
Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.prod)
Docs: Authenticate with Firebase on iOS using a Phone Number
API Reference: FirebaseAuth Framework Reference - FIRAuthAPNSTokenType
I have a Swift 2 iOS8+ app where I need to make a request to fetch JSON data when my app receives a push notification.
When the user clicks on the notification the app will go and fetch the data but I really need the data to be fetched as soon as the notification is received. This looks to be possible, is this the case?
I've implemented:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)){
and checking the launch options in:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
I've also enabled:
background fetch
remote notifications
None of this seems to help. If this is possible I'd be grateful for any pointers/tutorials on this.
As of iOS 9, instead of a normal push, you can use silent push notifications. When your app receives a silent push, the user is not notified, but your app can perform actions based on this notification. Then, when your background actions is finished, you create a local notification for the user.
Check out this tutorial for info on how to use silent notifications:
https://www.raywenderlich.com/123862/push-notifications-tutorial