@stryberventures/stryber-rn-core-components-push-notifications-service
v0.0.2
Published
## Description ##
Downloads
4
Keywords
Readme
Stryber React Native Push Notifications Service
Description
Service which has common API to work with Firebase Cloud Messages and local push notifications on Android and iOS. By default, FCM SDK shows Push Notifications which appeared when application was in background mode or in quit state. However, FCM SDK provides application with callback function, which tells that notification was received. It means if we want to show PNs when application is in foreground (active), we should create them locally when callback from FCM SDK was called.
This version of PushNotificationService shows PNs in all application states.
Installation
This module requires that the @react-native-firebase/app module is already setup and installed. To install the "app" module, view the Getting Started documentation.
Install dependencies
npm i @stryberventures/[email protected] @react-native-community/[email protected] @react-native-firebase/[email protected] [email protected] --save
Next steps are required only for iOS
Install pods
cd ios/ && pod install
Go through iOS installation guide, it describes how to:
- Enable push notifications in xcode.
- Enable background processes in xcode.
- How to create Push notification certificate and link it in Firebase console.
Navigate to ios/yourprojectname/AppDelegate.h
- add import
#import <UserNotifications/UNUserNotificationCenter.h>
- pass imported interface to UIResponder
@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate>
Navigate to ios/yourprojectname/AppDelegate.m
- add import
#import <UserNotifications/UserNotifications.h>
#import <RNCPushNotificationIOS.h>
- add next code before return of didFinishLaunchingWithOptions
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
- add next code in the AppDelegate implementation (before @end)
// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
// Required for the notification event. You must call the completion handler after handling the remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
[RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
[RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
}
// Required for localNotification event
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler
{
[RNCPushNotificationIOS didReceiveNotificationResponse:response];
}
//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
}
Installation guides and troubleshooting:
React Native Push Notifications
React Native Push Notifications iOS
Known issues
- messaging().getInitialNotification() on Android payload is empty
- Firebase console doesn't show if local notification was opened