Skip to content

Commit 34b4b28

Browse files
authored
Add method getTitle to get notification title in JS (#157)
1 parent 7a1531b commit 34b4b28

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

README.md

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ yarn add @react-native-community/push-notification-ios
1717

1818
## Link
1919

20-
21-
2220
### React Native v0.60+
2321

24-
The package is [automatically linked](https://github.com/react-native-community/cli/blob/master/docs/autolinking.md) when building the app. All you need to do is:
22+
The package is [automatically linked](https://github.com/react-native-community/cli/blob/master/docs/autolinking.md) when building the app. All you need to do is:
2523

2624
```bash
2725
npx pod-install
@@ -32,42 +30,43 @@ For android, the package will be linked automatically on build.
3230
<details>
3331
<summary>For React Native version 0.59 or older</summary>
3432

35-
### React Native <= v0.59
33+
### React Native <= v0.59
3634

3735
```bash
3836
react-native link @react-native-community/push-notification-ios
3937
```
4038

4139
- upgrading to `react-native >= 0.60`
4240

43-
First, unlink the library. Then follow the instructions above.
41+
First, unlink the library. Then follow the instructions above.
4442

45-
```bash
46-
react-native unlink @react-native-community/push-notification-ios
47-
```
43+
```bash
44+
react-native unlink @react-native-community/push-notification-ios
45+
```
4846

4947
- manual linking
5048

51-
If you don't want to use the methods above, you can always [link the library manually](./docs/manual-linking.md).
52-
49+
If you don't want to use the methods above, you can always [link the library manually](./docs/manual-linking.md).
50+
5351
</details>
5452

5553
### Add Capabilities : Background Mode - Remote Notifications
5654

5755
Go into your MyReactProject/ios dir and open MyProject.xcworkspace workspace.
5856
Select the top project "MyProject" and select the "Signing & Capabilities" tab.
5957
Add a 2 new Capabilities using "+" button:
58+
6059
- `Background Mode` capability and tick `Remote Notifications`.
6160
- `Push Notifications` capability
6261

63-
6462
### Augment `AppDelegate`
6563

6664
Finally, to enable support for `notification` and `register` events you need to augment your AppDelegate.
6765

68-
### Update `AppDelegate.h`
66+
### Update `AppDelegate.h`
6967

7068
At the top of the file:
69+
7170
```objective-c
7271
#import <UserNotifications/UNUserNotificationCenter.h>
7372
```
@@ -77,6 +76,7 @@ Then, add the 'UNUserNotificationCenterDelegate' to protocols:
7776
```objective-c
7877
@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate>
7978
```
79+
8080
### Update `AppDelegate.m`
8181
8282
At the top of the file:
@@ -151,16 +151,15 @@ And then in your AppDelegate implementation, add the following:
151151
This module was created when the PushNotificationIOS was split out from the core of React Native. To migrate to this module you need to follow the installation instructions above and then change you imports from:
152152

153153
```js
154-
import { PushNotificationIOS } from "react-native";
154+
import {PushNotificationIOS} from 'react-native';
155155
```
156156

157157
to:
158158

159159
```js
160-
import PushNotificationIOS from "@react-native-community/push-notification-ios";
160+
import PushNotificationIOS from '@react-native-community/push-notification-ios';
161161
```
162162

163-
164163
# Reference
165164

166165
## Methods
@@ -523,6 +522,16 @@ Gets the notification's main message from the `aps` object
523522

524523
---
525524

525+
### `getTitle()`
526+
527+
```jsx
528+
getTitle();
529+
```
530+
531+
Gets the notification's title from the `aps` object
532+
533+
---
534+
526535
### `getContentAvailable()`
527536

528537
```jsx

index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export interface PushNotification {
2929
*/
3030
getAlert(): string | Record<string, any>;
3131

32+
/**
33+
* Gets the notification's title from the `aps` object
34+
*/
35+
getTitle(): string;
36+
3237
/**
3338
* Gets the content-available number from the `aps` object
3439
*/

js/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export type PushNotificationEventName = $Keys<{
6969
class PushNotificationIOS {
7070
_data: Object;
7171
_alert: string | Object;
72+
_title: string;
7273
_sound: string;
7374
_category: string;
7475
_contentAvailable: ContentAvailable;
@@ -384,6 +385,7 @@ class PushNotificationIOS {
384385
const notifVal = nativeNotif[notifKey];
385386
if (notifKey === 'aps') {
386387
this._alert = notifVal.alert;
388+
this._title = notifVal?.alertTitle;
387389
this._sound = notifVal.sound;
388390
this._badgeCount = notifVal.badge;
389391
this._category = notifVal.category;
@@ -399,6 +401,7 @@ class PushNotificationIOS {
399401
this._badgeCount = nativeNotif.applicationIconBadgeNumber;
400402
this._sound = nativeNotif.soundName;
401403
this._alert = nativeNotif.alertBody;
404+
this._title = nativeNotif?.alertTitle;
402405
this._data = nativeNotif.userInfo;
403406
this._category = nativeNotif.category;
404407
this._fireDate = nativeNotif.fireDate;
@@ -466,6 +469,14 @@ class PushNotificationIOS {
466469
return this._alert;
467470
}
468471

472+
/**
473+
* Gets the notification's title from the `aps` object
474+
*
475+
*/
476+
getTitle(): ?string | ?Object {
477+
return this._title;
478+
}
479+
469480
/**
470481
* Gets the content-available number from the `aps` object
471482
*

0 commit comments

Comments
 (0)