Skip to content

Commit

Permalink
feat(ios): make didRegisterForRemoteNotificationsWithDeviceToken hand…
Browse files Browse the repository at this point in the history
…le Data and String push tokens (#2078)
  • Loading branch information
bibyzan authored and jcesarmobile committed Dec 11, 2019
1 parent 473a50e commit 94879b3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
26 changes: 18 additions & 8 deletions ios/Capacitor/Capacitor/Plugins/PushNotifications.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import Foundation
import UserNotifications

enum PushNotificationError: Error {
case tokenParsingFailed
}

/**
* Implement three common modal types: alert, confirm, and prompt
*/
Expand Down Expand Up @@ -74,16 +78,22 @@ public class CAPPushNotificationsPlugin : CAPPlugin {
@objc func listChannels(_ call: CAPPluginCall) {
call.unimplemented()
}

@objc public func didRegisterForRemoteNotificationsWithDeviceToken(notification: NSNotification){
guard let deviceToken = notification.object as? Data else {
return
if let deviceToken = notification.object as? Data {
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
notifyListeners("registration", data:[
"value": deviceTokenString
])
} else if let stringToken = notification.object as? String {
notifyListeners("registration", data:[
"value": stringToken
])
} else {
notifyListeners("registrationError", data: [
"error": PushNotificationError.tokenParsingFailed.localizedDescription
])
}
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
notifyListeners("registration", data:[
"value": deviceTokenString
])

}

@objc public func didFailToRegisterForRemoteNotificationsWithError(notification: NSNotification){
Expand Down
14 changes: 14 additions & 0 deletions site/docs-md/guides/push-notifications-firebase.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,20 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
return true
}
```
If you would like to recieve the firebase FCM token from iOS instead of the raw APNS token, you will need to also change your `AppDelegate.didRegisterForRemoteNotificationsWithDeviceToken` code to look like this:
```swift
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
InstanceID.instanceID().instanceID { (result, error) in
if let error = error {
NotificationCenter.default.post(name: Notification.Name(CAPNotifications.DidFailToRegisterForRemoteNotificationsWithError.name()), object: error)
} else if let result = result {
NotificationCenter.default.post(name: Notification.Name(CAPNotifications.DidRegisterForRemoteNotificationsWithDeviceToken.name()), object: result.token)
}
}
}
```


### Upload the APNS Certificate or Key to Firebase

Expand Down

0 comments on commit 94879b3

Please sign in to comment.