0.0.2 • Published 3 years ago

@stryberventures/stryber-rn-core-components-push-notifications-service v0.0.2

Weekly downloads
5
License
-
Repository
-
Last release
3 years ago

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/stryber-rn-core-components-push-notifications-service@0.0.1 @react-native-community/push-notification-ios@1.7.5-beta.0 @react-native-firebase/messaging@10.0.0 react-native-push-notification@6.1.3 --save

Next steps are required only for iOS

Install pods

cd ios/ && pod install

Go through iOS installation guide, it describes how to: 1. Enable push notifications in xcode. 2. Enable background processes in xcode. 3. 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:

RN wrapper for Firebase SDK

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