Skip to content

Commit 34dfb6b

Browse files
authored
feat(messaging): add apnsTokenReceived listener (#964)
* feat(messaging): add `getApnsToken` method (#602) * fix(messaging): use uppercase hex for APNs token * fix(messaging): reject when APNs token unavailable and document hex format * refactor(messaging): extract APNs token error message to constant * revert: remove getApnsToken method in favor of listener approach * feat(messaging): add `apnsTokenReceived` listener (#602) * style(messaging): format code * style: format * Revert "style: format" This reverts commit 1e89351.
1 parent 745e066 commit 34dfb6b

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@capacitor-firebase/messaging': minor
3+
---
4+
5+
feat: add `apnsTokenReceived` listener

packages/messaging/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ const removeAllListeners = async () => {
283283
* [`addListener('tokenReceived', ...)`](#addlistenertokenreceived-)
284284
* [`addListener('notificationReceived', ...)`](#addlistenernotificationreceived-)
285285
* [`addListener('notificationActionPerformed', ...)`](#addlistenernotificationactionperformed-)
286+
* [`addListener('apnsTokenReceived', ...)`](#addlistenerapnstokenreceived-)
286287
* [`removeAllListeners()`](#removealllisteners)
287288
* [Interfaces](#interfaces)
288289
* [Type Aliases](#type-aliases)
@@ -596,6 +597,28 @@ Only available for Android and iOS.
596597
--------------------
597598

598599

600+
### addListener('apnsTokenReceived', ...)
601+
602+
```typescript
603+
addListener(eventName: 'apnsTokenReceived', listenerFunc: ApnsTokenReceivedListener) => Promise<PluginListenerHandle>
604+
```
605+
606+
Called when the APNs token is received.
607+
608+
Only available for iOS.
609+
610+
| Param | Type |
611+
| ------------------ | ------------------------------------------------------------------------------- |
612+
| **`eventName`** | <code>'apnsTokenReceived'</code> |
613+
| **`listenerFunc`** | <code><a href="#apnstokenreceivedlistener">ApnsTokenReceivedListener</a></code> |
614+
615+
**Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt;</code>
616+
617+
**Since:** 8.2.0
618+
619+
--------------------
620+
621+
599622
### removeAllListeners()
600623

601624
```typescript
@@ -743,6 +766,13 @@ Remove all listeners for this plugin.
743766
| **`notification`** | <code><a href="#notification">Notification</a></code> | The notification in which the action was performed. | 0.2.2 |
744767

745768

769+
#### ApnsTokenReceivedEvent
770+
771+
| Prop | Type | Description | Since |
772+
| ----------- | ------------------- | --------------------------------------------------------- | ----- |
773+
| **`token`** | <code>string</code> | The native APNs token as an uppercase hex-encoded string. | 8.2.0 |
774+
775+
746776
### Type Aliases
747777

748778

@@ -777,6 +807,13 @@ Callback to receive the notification action performed event.
777807
<code>(event: <a href="#notificationactionperformedevent">NotificationActionPerformedEvent</a>): void</code>
778808

779809

810+
#### ApnsTokenReceivedListener
811+
812+
Callback to receive the APNs token received event.
813+
814+
<code>(event: <a href="#apnstokenreceivedevent">ApnsTokenReceivedEvent</a>): void</code>
815+
816+
780817
### Enums
781818

782819

packages/messaging/ios/Plugin/FirebaseMessagingPlugin.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class FirebaseMessagingPlugin: CAPPlugin, CAPBridgedPlugin {
3434
public let tokenReceivedEvent = "tokenReceived"
3535
public let notificationReceivedEvent = "notificationReceived"
3636
public let notificationActionPerformedEvent = "notificationActionPerformed"
37+
public let apnsTokenReceivedEvent = "apnsTokenReceived"
3738
public let errorTopicMissing = "topic must be provided."
3839
public let errorNotificationsMissing = "notifications must be provided."
3940
public let errorNotificationsInvalid = "The provided notifications are invalid."
@@ -221,6 +222,10 @@ public class FirebaseMessagingPlugin: CAPPlugin, CAPBridgedPlugin {
221222
return
222223
}
223224
Messaging.messaging().apnsToken = deviceToken
225+
let token = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
226+
var result = JSObject()
227+
result["token"] = token
228+
notifyListeners(apnsTokenReceivedEvent, data: result, retainUntilConsumed: true)
224229
}
225230

226231
@objc private func didReceiveRemoteNotification(notification: NSNotification) {

packages/messaging/src/definitions.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ export interface FirebaseMessagingPlugin {
176176
eventName: 'notificationActionPerformed',
177177
listenerFunc: NotificationActionPerformedListener,
178178
): Promise<PluginListenerHandle>;
179+
/**
180+
* Called when the APNs token is received.
181+
*
182+
* Only available for iOS.
183+
*
184+
* @since 8.2.0
185+
*/
186+
addListener(
187+
eventName: 'apnsTokenReceived',
188+
listenerFunc: ApnsTokenReceivedListener,
189+
): Promise<PluginListenerHandle>;
179190
/**
180191
* Remove all listeners for this plugin.
181192
*
@@ -454,6 +465,13 @@ export type NotificationActionPerformedListener = (
454465
event: NotificationActionPerformedEvent,
455466
) => void;
456467

468+
/**
469+
* Callback to receive the APNs token received event.
470+
*
471+
* @since 8.2.0
472+
*/
473+
export type ApnsTokenReceivedListener = (event: ApnsTokenReceivedEvent) => void;
474+
457475
/**
458476
* @since 0.2.2
459477
*/
@@ -464,6 +482,18 @@ export interface TokenReceivedEvent {
464482
token: string;
465483
}
466484

485+
/**
486+
* @since 8.2.0
487+
*/
488+
export interface ApnsTokenReceivedEvent {
489+
/**
490+
* The native APNs token as an uppercase hex-encoded string.
491+
*
492+
* @since 8.2.0
493+
*/
494+
token: string;
495+
}
496+
467497
/**
468498
* @since 0.2.2
469499
*/

0 commit comments

Comments
 (0)