1.0.0 • Published 4 years ago
ws-react-native-push-notifications v1.0.0
ws-react-native-push-notifications
Push notifications for all Wealthsimple mobile apps
Installation
JS
yarn add ws-react-native-push-notifications
iOS
- create 2 apps (one for dev, one for prod) in SFMC: https://mc.s7.exacttarget.com/cloud/#app/MobilePush/mobilepush/%23!/administration
- download APNS_Auth_key_JBC25YL3ZD.p8 from 1password
- upload .p8 file to SFMC app admin
- set Key ID (JBC25YL3ZD), Team ID (8994AF9LL2), and Bundle ID
- In Xcode App > Target > Signing & Capabilities > + Capability
- Push Notifications
- Background Modes > Remote notifications
- In AppDelegate.m
- add this import:
#import "WsReactNativePushNotifications.h"
- in
didFinishLaunchingWithOptions
add this line:
[[WsReactNativePushNotifications sharedInstance] setupWithAppId:@"<app id>" withAccessToken:@"<access token>" withServerUrl:@"<endpoint>" withMid:@"<mid>"];
- add these lines:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[[WsReactNativePushNotifications sharedInstance] setDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"didFailToRegisterForRemoteNotificationsWithError %@", error);
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[[WsReactNativePushNotifications sharedInstance] didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
- Run your app and get the device token from the logs (search for
setDeviceToken
) - Test the SFMC ↔ App setup by sending a test push notification in SFMC
- If that didn't work, test the APNS ↔ App setup by running this command:
./node_modules/ws-react-native-push-notifications/sendPush <deviceToken> <pathToP8>
(replace with the token found in the previous step and you can with the path of the .p8 Auth Key file). You should receive a push notification.
Android
- Create staging and production projects in Firebase if they don’t exist
- Setup Firebase by following these instructions
- Project Settings > Cloud Messaging > copy Server Key and Sender ID
- Enter the server key in both SFMC apps' settings (same apps as iOS)
- Add this line in your top level
build.gradle
insideallprojects { repositories { } }
:
maven { url "https://salesforce-marketingcloud.github.io/MarketingCloudSDK-Android/repository" }
- In
MainApplication.java
- add this import:
import com.wsreactnativepushnotifications.PushService;
- in
onCreate
add this lines (after Firebase setup if you have one):
PushService.INSTANCE.setup(this, "<fcm_sender_id>", "<app_id>", "<access_token>", "<server_url>", "<mid>", R.drawable.ic_notification);
Usage
import WsReactNativePushNotifications from 'ws-react-native-push-notifications';
Get Permission status
undertermined
(iOS only): the user hasn't answered the prompt yetauthorized
: the user has granted the permissiondenied
: the user has denied the permission in the OS settings, or answered the prompt with "Don't allow"
const status = await WsReactNativePushNotifications.getPermissionStatus();
Request the notification permission.
If on iOS and getPermissionStatus()
is "undetermined"
, it will show the native prompt.
Else, it won't do anything.
To redirect the user to the app settings, call Linking.openSettings();
await WsReactNativePushNotifications.requestPermission();
Identify the user
To identify the user using their identity canonical id on app start, login or sign up:
WsReactNativePushNotifications.setIdentityCanonicalId('identity-123456');
Open the app settings
To redirect the user to the app's settings screen in the OS:
WsReactNativePushNotifications.openAppSettings();
1.0.0
4 years ago