Manage pusher interest subscriptions from within React Native JS
IMPORTANT!!! This module is intended to complement the default Pusher setup. This module simply allows that implementation to be accessed directly from React Native JS.
$ npm install react-native-pusher-push-notifications --save
$ react-native link react-native-pusher-push-notifications
- In XCode, in the project navigator, right click
Libraries
➜Add Files to [your project's name]
- Go to
node_modules
➜react-native-pusher-push-notifications
and addRNPusherPushNotifications.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNPusherPushNotifications.a
to your project'sBuild Phases
➜Link Binary With Libraries
- Run your project (
Cmd+R
)<
- Open up
android/app/src/main/java/[...]/MainActivity.java
- Add
import com.reactlibrary.RNPusherPushNotificationsPackage;
to the imports at the top of the file - Add
new RNPusherPushNotificationsPackage()
to the list returned by thegetPackages()
method
- Append the following lines to
android/settings.gradle
:include ':react-native-pusher-push-notifications' project(':react-native-pusher-push-notifications').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-react-native-pusher-push-notifications/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-pusher-push-notifications')
** DO NOT follow the pusher.com push notification docs that detail modifying the AppDelegate.h/m files! - this package takes care of most of the steps for you**
- After package installation open
AppDelegate.h
and add:
#import <RNPusherPushNotifications.h>
// ..after @implements and before @end
@property (nonatomic, strong) RNPusherPushNotifications *RNPusher;
- Open
AppDelegate.m
and add:
// Add the following as a new method to AppDelegate.m
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[[((RCTRootView *) self.window.rootViewController.view).bridge moduleForName:@"RNPusherPushNotifications"] setDeviceToken:deviceToken];
}
// Import module
import RNPusherPushNotifications from 'react-native-pusher-push-notifications';
// Get your interest
const interest = "donuts";
// Set your app key and register for push
RNPusherPushNotifications.setAppKey(ENV.PUSHER_APP_KEY);
if (Platform.OS === 'ios') {
// iOS must wait for rego
RNPusherPushNotifications.on('registered', initInterests)
} else {
// Android is immediate
initInterests()
}
function initInterests() {
// Subscribe to push notifications
if (Platform.OS === 'ios') {
// iOS callbacks are beta, so dont use them
RNPusherPushNotifications.subscribe(interest);
} else {
// Android is better, so handle faults
RNPusherPushNotifications.subscribe(
interest,
(error) => {
console.error(error);
},
(success) => {
console.log(success);
}
);
}
}
// Unsubscribe from push notifications
if (Platform.OS === 'ios') {
// iOS callbacks are beta, so dont use them
RNPusherPushNotifications.unsubscribe(interest);
} else {
// Android is better, so handle faults
RNPusherPushNotifications.unsubscribe(
interest,
(error) => {
console.error(error);
},
(success) => {
console.log(success);
}
);
}