|
| 1 | +# React Native VoIP Push Notification |
| 2 | +React Native VoIP Push Notification - Currently iOS >= 8.0 only |
| 3 | + |
| 4 | +## Motivation |
| 5 | + |
| 6 | +Since iOS 8.0 there is an execellent feature called **VoIP Push Notification** ([PushKit][1]), while in React Native only the traditional push notification is supported which limits the possibilities of building a VoIP app with React Native (like me!). |
| 7 | + |
| 8 | +To understand the benefits of **Voip Push Notification**, please see [VoIP Best Practices][2]. |
| 9 | + |
| 10 | +**Note 1**: Not sure if Android support this sort of stuff since I'm neither an iOS nor Android expert, from my limited understanding that GCM's [sending high priority push notification][5] might be the case. Correct me if I'm wrong! |
| 11 | + |
| 12 | +**Note 2** This module is inspired by [PushNotificationIOS][6] and [React Native Push Notification][7] |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install --save react-native-voip-push-notification |
| 18 | +``` |
| 19 | + |
| 20 | +### iOS |
| 21 | + |
| 22 | +The iOS version should be >= 8.0 since we are using [PushKit][1]. |
| 23 | + |
| 24 | +#### AppDelegate.m Modification |
| 25 | + |
| 26 | +```objective-c |
| 27 | + |
| 28 | +... |
| 29 | + |
| 30 | +#import <PushKit/PushKit.h> /* <------ add this line */ |
| 31 | +#import "RNVoipPushNotificationManager.h" /* <------ add this line */ |
| 32 | + |
| 33 | +... |
| 34 | + |
| 35 | +@implementation AppDelegate |
| 36 | + |
| 37 | +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| 38 | +{ |
| 39 | + |
| 40 | +... |
| 41 | + |
| 42 | +/* Add PushKit delegate method */ |
| 43 | + |
| 44 | +// Handle updated push credentials |
| 45 | +- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type { |
| 46 | + // Register VoIP push token (a property of PKPushCredentials) with server |
| 47 | + NSLog(@"[AppDelegate][VoIP] didUpdatePushCredentials credentials.token = %@, type = %@", credentials.token, type); |
| 48 | + [RNVoipPushNotificationManager didUpdatePushCredentials:credentials forType:(NSString *)type]; |
| 49 | +} |
| 50 | + |
| 51 | +// Handle incoming pushes |
| 52 | +- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type { |
| 53 | + // Process the received push |
| 54 | + NSLog(@"[AppDelegate][VoIP] didReceiveIncomingPushWithPayload payload.dictionaryPayload = %@, type = %@", payload.dictionaryPayload, type); |
| 55 | + [RNVoipPushNotificationManager didReceiveIncomingPushWithPayload:payload forType:(NSString *)type]; |
| 56 | +} |
| 57 | + |
| 58 | +... |
| 59 | + |
| 60 | +@end |
| 61 | + |
| 62 | +``` |
| 63 | +
|
| 64 | +#### Add PushKit Framework |
| 65 | +
|
| 66 | +- In your Xcode project, select `Build Phases` --> `Link Binary With Libraries` |
| 67 | +- Add `PushKit.framework` |
| 68 | +
|
| 69 | +#### Add RNVoipPushNotification |
| 70 | +
|
| 71 | +##### Option 1: Use [rnpm][3] |
| 72 | +
|
| 73 | +```bash |
| 74 | +rnpm link react-native-voip-push-notification |
| 75 | +``` |
| 76 | + |
| 77 | +**Note**: If you're using rnpm link make sure the `Header Search Paths` is `recursive`. (In step 3 of manually linking) |
| 78 | + |
| 79 | +##### Option 2: Manually |
| 80 | + |
| 81 | +1. Drag `node_modules/react-native-voip-push-notification/ios/RNVoipPushNotification.xcodeproj` under `<your_xcode_project>/Libraries` |
| 82 | +2. Select `<your_xcode_project>` --> `Build Phases` --> `Link Binary With Libraries` |
| 83 | + - Drag `Libraries/RNVoipPushNotification.xcodeproj/Products/libRNVoipPushNotification.a` to `Link Binary With Libraries` |
| 84 | +3. Select `<your_xcode_project>` --> `Build Settings` |
| 85 | + - In `Header Search Paths`, add `$(SRCROOT)/../node_modules/react-native-voip-push-notification/ios/RNVoipPushNotification` with `recursive` |
| 86 | + |
| 87 | +## Usage |
| 88 | + |
| 89 | +```javascript |
| 90 | + |
| 91 | +... |
| 92 | + |
| 93 | +import VoipPushNotification from 'react-native-voip-push-notification'; |
| 94 | + |
| 95 | +... |
| 96 | + |
| 97 | +class MyComponent extends React.Component { |
| 98 | + |
| 99 | +... |
| 100 | + |
| 101 | + componentWillMount() { // or anywhere which is most comfortable and appropriate for you |
| 102 | + VoipPushNotification.requestPermissions(); // required |
| 103 | + |
| 104 | + VoipPushNotification.addEventListener('register', (token) => { |
| 105 | + // send token to your apn provider server |
| 106 | + }); |
| 107 | + |
| 108 | + VoipPushNotification.addEventListener('notification', (notification) => { |
| 109 | + // register your VoIP client, show local notification, etc. |
| 110 | + // e.g. |
| 111 | + this.doRegister(); |
| 112 | + |
| 113 | + /** |
| 114 | + * Local Notification Payload |
| 115 | + * |
| 116 | + * - `alertBody` : The message displayed in the notification alert. |
| 117 | + * - `alertAction` : The "action" displayed beneath an actionable notification. Defaults to "view"; |
| 118 | + * - `soundName` : The sound played when the notification is fired (optional). |
| 119 | + * - `category` : The category of this notification, required for actionable notifications (optional). |
| 120 | + * - `userInfo` : An optional object containing additional notification data. |
| 121 | + */ |
| 122 | + VoipPushNotification.presentLocalNotification({ |
| 123 | + alertBody: "hello! " + notification.getMessage() |
| 124 | + }); |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | +... |
| 129 | + |
| 130 | +} |
| 131 | + |
| 132 | +``` |
| 133 | + |
| 134 | +## Contributing |
| 135 | + |
| 136 | +Any pull request, issue report and suggestion are highly welcome! |
| 137 | + |
| 138 | +## License |
| 139 | + |
| 140 | +[ISC License][4] (functionality equivalent to **MIT License**) |
| 141 | + |
| 142 | +[1]: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Reference/PushKit_Framework/index.html |
| 143 | +[2]: https://developer.apple.com/library/ios/documentation/Performance/Conceptual/EnergyGuide-iOS/OptimizeVoIP.html |
| 144 | +[3]: https://github.com/rnpm/rnpm |
| 145 | +[4]: https://opensource.org/licenses/ISC |
| 146 | +[5]: https://developers.google.com/cloud-messaging/concept-options#setting-the-priority-of-a-message |
| 147 | +[6]: https://facebook.github.io/react-native/docs/pushnotificationios.html |
| 148 | +[7]: https://github.com/zo0r/react-native-push-notification |
0 commit comments