|
| 1 | +# Android Installation Guide |
| 2 | + |
| 3 | +## Prerequisites |
| 4 | + |
| 5 | +The documentation that follows assumes you have generated a React Native Android project using the `react-native-cli`, i.e.: |
| 6 | + |
| 7 | +``` |
| 8 | +react-native init myapp |
| 9 | +``` |
| 10 | + |
| 11 | +In addition to the standard React Native requirements, you will also need to install the following Android SDK components with your prefered SDK management tools: |
| 12 | +* Google Play services |
| 13 | + |
| 14 | +## Install react-native-azurenotificationhub |
| 15 | + |
| 16 | +``` |
| 17 | +npm install react-native-azurenotificationhub |
| 18 | +``` |
| 19 | + |
| 20 | +## Create a Notification Hub |
| 21 | + |
| 22 | +* Log on to the [Azure Portal](https://portal.azure.com) and create a new **Notification Hub**. |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +## Register app with Notification Hub |
| 27 | + |
| 28 | +* Log in to the [Firebase console](https://firebase.google.com/console/) and create a new Firebase project if you don't already have one. |
| 29 | +* After your project is created click **Add Firebase to your Android app** and folow the instructions provided. |
| 30 | + |
| 31 | +. |
| 32 | + |
| 33 | +* In the Firebase Console, click the cog for your project and then click **Project Settings** |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +* Click the **Cloud Messaging** tab in your project settings and copy the value of the **Server key** and **Sender ID**. The former will be used to configure the Notification Hub Access Policy and and the latter for your React Native module registration. |
| 38 | + |
| 39 | +* Back on the [Azure Portal](https://portal.azure.com) page for your notification hub, select **Settings > Notification Services > Google (GCM)**. Enter the FCM **Server key** you copied from the [Firebase console](https://firebase.google.com/console/) and click **Save**. |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +## Merging of icon resources |
| 44 | + |
| 45 | +In `android/app/src/main/AndroidManifest.xml` |
| 46 | + |
| 47 | +```xml |
| 48 | + <application |
| 49 | + xmlns:tools="http://schemas.android.com/tools" |
| 50 | + tools:replace="android:icon,android:allowBackup" |
| 51 | + ...> |
| 52 | + </application> |
| 53 | +``` |
| 54 | + |
| 55 | +This resolves the error caused by the manifest merger tool for gradle. |
| 56 | + |
| 57 | +## Export React Native Module from app |
| 58 | +In `android/build.gradle` |
| 59 | + |
| 60 | +```gradle |
| 61 | +... |
| 62 | +
|
| 63 | +buildscript { |
| 64 | + ... |
| 65 | + dependencies { |
| 66 | + ... |
| 67 | + classpath("com.google.gms:google-services:4.2.0") |
| 68 | + } |
| 69 | +} |
| 70 | +
|
| 71 | +``` |
| 72 | + |
| 73 | +In `android/app/build.gradle` |
| 74 | + |
| 75 | +```gradle |
| 76 | +... |
| 77 | +dependencies { |
| 78 | + ... |
| 79 | +
|
| 80 | + implementation project(":react-native-azurenotificationhub") // <- Note only include this line if using a version of RN < 0.60 since it will be auto linked |
| 81 | + implementation "com.google.firebase:firebase-messaging:17.6.0" |
| 82 | + implementation "com.google.firebase:firebase-core:16.0.8" |
| 83 | +} |
| 84 | +
|
| 85 | +apply plugin: "com.google.gms.google-services" |
| 86 | +
|
| 87 | +``` |
| 88 | + |
| 89 | +In `android/app/src/main/AndroidManifest.xml` |
| 90 | + |
| 91 | +```xml |
| 92 | + ... |
| 93 | + |
| 94 | + <uses-permission android:name="android.permission.INTERNET"/> |
| 95 | + |
| 96 | + <application ...> |
| 97 | + ... |
| 98 | + <uses-library |
| 99 | + android:name="org.apache.http.legacy" |
| 100 | + android:required="false" /> |
| 101 | + |
| 102 | + <service |
| 103 | + android:name="com.azure.reactnative.notificationhub.ReactNativeRegistrationIntentService" |
| 104 | + android:exported="false" /> |
| 105 | + |
| 106 | + <service |
| 107 | + android:name="com.azure.reactnative.notificationhub.ReactNativeFirebaseMessagingService" |
| 108 | + android:stopWithTask="false"> |
| 109 | + <intent-filter> |
| 110 | + <action android:name="com.google.firebase.MESSAGING_EVENT" /> |
| 111 | + </intent-filter> |
| 112 | + </service> |
| 113 | + |
| 114 | + <receiver |
| 115 | + android:name="com.microsoft.windowsazure.notifications.NotificationsBroadcastReceiver" |
| 116 | + android:permission="com.google.android.c2dm.permission.SEND"> |
| 117 | + <intent-filter> |
| 118 | + <action android:name="com.google.android.c2dm.intent.RECEIVE" /> |
| 119 | + </intent-filter> |
| 120 | + </receiver> |
| 121 | + ... |
| 122 | +``` |
| 123 | + |
| 124 | +## If using a version of React Native before [RN 0.60](https://github.com/facebook/react-native/releases/tag/v0.60.0) that does not support autolinking: |
| 125 | + |
| 126 | +In `android/settings.gradle` |
| 127 | + |
| 128 | +```gradle |
| 129 | +... |
| 130 | +
|
| 131 | +include ':react-native-azurenotificationhub' |
| 132 | +project(':react-native-azurenotificationhub').projectDir = file('../node_modules/react-native-azurenotificationhub/android') |
| 133 | +``` |
| 134 | + |
| 135 | +Register the module package in `MainApplication.java` |
| 136 | + |
| 137 | +```java |
| 138 | +import com.azure.reactnative.notificationhub.ReactNativeNotificationHubPackage; |
| 139 | + |
| 140 | +public class MainApplication extends Application implements ReactApplication { |
| 141 | + |
| 142 | + private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { |
| 143 | + @Override |
| 144 | + protected boolean getUseDeveloperSupport() { |
| 145 | + return BuildConfig.DEBUG; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + protected List<ReactPackage> getPackages() { |
| 150 | + return Arrays.<ReactPackage>asList( |
| 151 | + new MainReactPackage(), |
| 152 | + new ReactNativeNotificationHubPackage() // <-- Add this package |
| 153 | + ); |
| 154 | + } |
| 155 | + }; |
| 156 | + |
| 157 | + ... |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +## JavaScript Configuration |
| 162 | + |
| 163 | +On the [Azure Portal](https://portal.azure.com) page for your notification hub, copy a connection string from **Settings > Access Policies**. |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | +The example below shows how you can register and unregister from Azure Notification Hub in your React component. |
| 168 | + |
| 169 | +```js |
| 170 | +const NotificationHub = require('react-native-azurenotificationhub'); |
| 171 | +const PushNotificationEmitter = new NativeEventEmitter(NotificationHub); |
| 172 | + |
| 173 | +const NOTIF_REGISTER_AZURE_HUB_EVENT = 'azureNotificationHubRegistered'; |
| 174 | +const NOTIF_AZURE_HUB_REGISTRATION_ERROR_EVENT = 'azureNotificationHubRegistrationError'; |
| 175 | +const DEVICE_NOTIF_EVENT = 'remoteNotificationReceived'; |
| 176 | + |
| 177 | +const connectionString = '...'; // The Notification Hub connection string |
| 178 | +const hubName = '...'; // The Notification Hub name |
| 179 | +const senderID = '...'; // The Sender ID from the Cloud Messaging tab of the Firebase console |
| 180 | +const tags = [ ... ]; // The set of tags to subscribe to |
| 181 | + |
| 182 | +class myapp extends Component { |
| 183 | + constructor(props) { |
| 184 | + super(props); |
| 185 | + PushNotificationEmitter.addListener(DEVICE_NOTIF_EVENT, this._onRemoteNotification); |
| 186 | + } |
| 187 | + |
| 188 | + register() { |
| 189 | + PushNotificationEmitter.addListener(NOTIF_REGISTER_AZURE_HUB_EVENT, this._onAzureNotificationHubRegistered); |
| 190 | + PushNotificationEmitter.addListener(NOTIF_AZURE_HUB_REGISTRATION_ERROR_EVENT, this._onAzureNotificationHubRegistrationError); |
| 191 | + |
| 192 | + NotificationHub.register({connectionString, hubName, senderID, tags}) |
| 193 | + .catch(reason => console.warn(reason)); |
| 194 | + } |
| 195 | + |
| 196 | + unregister() { |
| 197 | + NotificationHub.unregister() |
| 198 | + .catch(reason => console.warn(reason)); |
| 199 | + } |
| 200 | + |
| 201 | + render() { |
| 202 | + return ( |
| 203 | + <View style={styles.container}> |
| 204 | + <TouchableOpacity onPress={this.register.bind(this)}> |
| 205 | + <View style={styles.button}> |
| 206 | + <Text style={styles.buttonText}> |
| 207 | + Register |
| 208 | + </Text> |
| 209 | + </View> |
| 210 | + </TouchableOpacity> |
| 211 | + <TouchableOpacity onPress={this.unregister.bind(this)}> |
| 212 | + <View style={styles.button}> |
| 213 | + <Text style={styles.buttonText}> |
| 214 | + Unregister |
| 215 | + </Text> |
| 216 | + </View> |
| 217 | + </TouchableOpacity> |
| 218 | + </View> |
| 219 | + ); |
| 220 | + } |
| 221 | + |
| 222 | + _onAzureNotificationHubRegistered(registrationID) { |
| 223 | + console.warn('RegistrationID: ' + registrationID); |
| 224 | + } |
| 225 | + |
| 226 | + _onAzureNotificationHubRegistrationError(error) { |
| 227 | + console.warn('Error: ' + error); |
| 228 | + } |
| 229 | + |
| 230 | + _onRemoteNotification(notification) { |
| 231 | + // Note notification will be a JSON string for android |
| 232 | + console.warn('Notification received: ' + notification); |
| 233 | + } |
| 234 | +``` |
0 commit comments