react-native-push-token
Getting started
$ npm install react-native-push-token --save
or
$ yarn add react-native-push-token
Mostly automatic installation
$ react-native link react-native-push-token
Manual installation
iOS
- In XCode, in the project navigator, right click
Libraries
➜Add Files to [your project's name]
- Go to
node_modules
➜react-native-push-token
and addRNReactNativePushToken.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNReactNativePushToken.a
to your project'sBuild Phases
➜Link Binary With Libraries
- Add the following snippet to your
AppDelegate.m
#import <RNPushToken.h>
...
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[RNPushToken didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
- Run your project (
Cmd+R
)<
Android
- Open up
android/app/src/main/java/[...]/MainActivity.java
- Add
import com.reactlibrary.RNPushTokenPackage;
to the imports at the top of the file - Add
new RNPushTokenPackage()
to the list returned by thegetPackages()
method with the senderID e.g.new RNPushTokenPackage(getApplicationContext().getString(R.string.senderID)),
- Append the following lines to
android/settings.gradle
:include ':react-native-push-token' project(':react-native-push-token').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-push-token/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-push-token')
- Add the following to the android manifest
<service android:name="sh.pixelbru.RNPushTokenService" />
NOTE: You may need to set a specific gcm/firebase version to your app's build.grade to ensure all versions across your deps are the same e.g.
compile ('com.google.firebase:firebase-messaging:11.0.2') { force = true } compile('com.google.android.gms:play-services-gcm:11.0.2') { force = true }
Windows
- In Visual Studio add the
RNPushToken.sln
innode_modules/react-native-push-token/windows/RNPushToken.sln
folder to their solution, reference from their app. - Open up your
MainPage.cs
app
- Add
using React.Native.Push.Token.RNPushToken;
to the usings at the top of the file - Add
new RNPushTokenPackage()
to theList<IReactPackage>
returned by thePackages
method
Usage
import RNPushToken from 'react-native-push-token';
try {
const token = await RNPushToken.getToken();
} catch (error) {
console.error(error);
}