oracle / pushiomanager-react-native

React Native Module for Responsys SDK
Universal Permissive License v1.0
15 stars 17 forks source link
android ios oracle react-native responsys

React Native Module for Responsys SDK

This module makes it easy to integrate your React Native based mobile app with the Responsys SDK.

Table of Contents

Requirements

For Android

For iOS

Setup

Before installing the plugin, you must setup your app to receive push notifications.

For Android

For iOS

Installation

The plugin can be installed with the React Native CLI,

cd <your_react_native_app>
yarn add @oracle/react-native-pushiomanager

For Android

For iOS

After installing plugin you need to install cocoapods,

Note: This step will fail if PushIOManager.xcframework is not available in YOUR_APP_DIR/ios/framework/ folder before adding plugin to project with npm or yarn. Copy the PushIOManager.xcframework to YOUR_APP_DIR/ios/framework/ and perform Installation step again.

Integration

For Android

For iOS

#import <PushIOManager/PushIOManager.h>
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:
    (NSData *)deviceToken
{
    [[PushIOManager sharedInstance]  didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    [[PushIOManager sharedInstance]  didFailToRegisterForRemoteNotificationsWithError:error];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [[PushIOManager sharedInstance] didReceiveRemoteNotification:userInfo];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:
(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    [[PushIOManager sharedInstance] didReceiveRemoteNotification:userInfo
fetchCompletionResult:UIBackgroundFetchResultNewData fetchCompletionHandler:completionHandler];
}

//iOS 10 or later
-(void) userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:
(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler
{
    [[PushIOManager sharedInstance] userNotificationCenter:center didReceiveNotificationResponse:response
withCompletionHandler:completionHandler];
}

-(void) userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:
(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
    [[PushIOManager sharedInstance] userNotificationCenter:center willPresentNotification:notification
withCompletionHandler:completionHandler];
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
   [[PushIOManager sharedInstance] openURL:url options:options];
  return YES;
}

Usage

The module can be accessed in JS code as follows,

import PushIOManager from '@oracle/react-native-pushiomanager';

Configure And Register

User Identification

Engagements And Conversion

User actions can be attributed to a push notification using,

PushIOManager.trackEngagement(3, properties, (error, response) => {

});

In-App Messages

In-App Message (IAM) are displayed in a popup window via system-defined triggers like $ExplicitAppOpen or custom triggers. IAM that use system-defined triggers are displayed automatically.

IAM can also be displayed on-demand using custom triggers.

These below steps are required for iOS In-App Messages.

Message Center

Each Message Center message now supports an additional property called custom key-value pairs, it is a variable sized object with key value pairs and can be accessed like any other property of that message.

```javascript
PushIOManager.fetchMessagesForMessageCenter(messageCenterName, (error, response) => {
        if(error == null) {
            for(message of response.messages){
                console.log(message.messageID)
                console.log(message.message)
                console.log(message.customKeyValuePairs)

            }
        }
});
```
componentDidMount() {
       this.messageCenterListener = PushIOManager.addMessageCenterUpdateListener((response) =>
        console.log(response);
      );
}
componentWillUnmount() {
    if (this.messageCenterListener) {
        this.messageCenterListener.remove();
    }
}

If your app uses Functional components, update the useEffect hook to store the listener

useEffect(() => {
    const messageCenterListener = PushIOManager.addMessageCenterUpdateListener((response) =>
        console.log(response);
      );

    return () => {
        messageCenterListener.remove()
   }; };

Geofences And Beacons

If your app is setup to monitor geofence and beacons, you can use the following APIs to record in Responsys when a user enters/exits a geofence/beacon zone.

PushIOManager.onGeoRegionEntered(geoRegion, (error, response) => {});
PushIOManager.onGeoRegionExited(geoRegion, (error, response) => {});
PushIOManager.onBeaconRegionEntered(beaconRegion, (error, response) => {});
PushIOManager.onBeaconRegionExited(beaconRegion, (error, response) => {});

Notification Preferences

Preferences are used to record user-choices for push notifications. The preferences should be pre-defined in Responsys before being used in your app.

Do not use this as a persistent (key/value) store since this data is purgeable.

Changing Notification Icon and Color (Android Only)

Responsys SDK uses the app icon as the icon for notifications. You can change this by using the following two APIs,

PushIOManager.setNotificationLargeIcon("ic_notification_large");
PushIOManager.setNotificationSmallIcon("ic_notification_small");

It is also possible to change the notification small icon color by using the following API,

PushIOManager.setNotificationSmallIconColor("#d1350f");

Handling Push Notifications (Android Only)

With release 6.52.1, you can now subscribe to FCM device tokens and push notifications callbacks from the Responsys plugin.

PushIOManager.onPushTokenReceived( response => {
    console.log("Device Token: " + response.deviceToken);
});

PushIOManager.onPushNotificationReceived( remoteMessage => {

    console.log("Push Message: " + JSON.stringify(remoteMessage));

    PushIOManager.isResponsysPush(remoteMessage, (error, response) => {

        if (response) {
            console.log("Received Push Message from Responsys");
            PushIOManager.handleMessage(remoteMessage);
        } else {
             // Not a Responsys Push, handle it appropriately
        }
    });
});

This will allow you to handle push notifications while the app is in foreground or background.

These callbacks are also useful if you have multiple push plugins and need to decide how to process the incoming push notifications.

NOTE: When the app is in killed state (i.e. not in memory) the Responsys plugin will automatically process and display the push notifications.

Handling Deeplinks

When the user taps on a push notification (having a deeplink), the plugin passes the deeplink to the app.

Your app must implement the following native code to handle deeplinks.

For iOS

Ensure that you have implemented openURL method in your AppDelegate.m, as follows,

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {

    [[PushIOManager sharedInstance] openURL:url options:options];

  return YES;
}

For Android

In your Javascript code, add the following deeplink listeners,

If your app uses React Class components, use lifecycle methods componentDidMount and componentWillUnmount to store and clean up the listener.

componentDidMount() {
    this.deepLinkListener = PushIOManager.setOpenURLListener(true, deeplink =>
        console.log("Deeplink: " + deeplink);
    );
}

componentWillUnmount() {
    if (this.deepLinkListener) {
        this.deepLinkListener.remove();
    }
}

If your app uses Functional components, update the useEffect hook to store listener

useEffect(() => {
    const deepLinkListener = PushIOManager.setOpenURLListener(true, deeplink =>{
        console.log("Deeplink: " + deeplink);
    });

    return () => {
        deepLinkListener.remove()
    };
});

If your app uses Functional components, update the react-natigation hook to store listener

const [isReady, setIsReady] = useState(false);

const config =  {
   initialRouteName: "Home",
   screens: {
     Home: {
       path: "home",
     }
   }
  };

const linking = {
    prefixes: ["example://"],
    config: config.
}

let deeplinkurl

const onReceivePushIOURL = (deeplink) => {

  if (Platform.OS === "ios") {
    deeplinkurl =  deeplink.url;
  } else {
    deeplinkurl =  deeplink;
  }

  console.log("onReceivePushIOURL deeplink: " + deeplinkurl);

  if (deeplinkurl && navigationRef.isReady) {
      Linking.openURL(deeplinkurl);
    }

};

 useEffect(() => {
    const deepLinkListener = PushIOManager.setOpenURLListener(true, onReceivePushIOURL);
   return () => {
     deepLinkListener.remove();
   };

 },[]);

 return (
  <NavigationContainer ref={navigationRef}  onReady={onNavigationReady} linking={linking}
  )

Reporting Revenue/Purchase Events

Responsys plugin version 6.48.0 and above supports reporting Revenue/Purchase events. These events attribute purchases to the push campaign that led the app user into the mobile app. This API accepts an event object which allows In-App Purchase (Digital Goods) or Retail Purchase (Physical Goods) conversion-type to be reported.

var event = {};
event['orderId'] = '1234';
event['orderTotal'] = 50;
event['orderQuantity'] = 5;
event['conversionType'] = 3; // Possible values: 3 = In-App Purchase, 7 = Retail Purchase
event['customProperties'] = {}; // optional

PushIOManager.trackConversionEvent(event, (error, response) => {

});

Upgrades

6.56.3

For Android

API for registration has been changed to: registerApp(enablePushNotification, useLocation, callback)

This allows your app to control when to display the push notification permission prompt on Android 13 and later devices.

Also, with this release, we have made changes to our Multiple SDK guide after reports of duplicate notifications displayed due to a bug in @react-native-firebase/messaging plugin.

Make sure you implement the updated guide as you upgrade to plugin v6.56.3.

6.52.1

For Android

If you have been following our Multiple SDK guide, there are some changes with this release.

We no longer need the @react-native-firebase/app and @react-native-firebase/messaging plugins for multiple push plugin scenarios or to receive push in the background.

So, you may choose to uninstall these plugins.

yarn remove @react-native-firebase/app
yarn remove @react-native-firebase/messaging

6.50.1 to 6.51

With the release of v6.51.0, we have simplified the plugin integration process.

Due to this change, you will need to perform the following steps one-time only.

For Android

For iOS

Support

If you have access to My Oracle Support, please raise a request here, otherwise open an issue in this repository.

Contributing

This project welcomes contributions from the community. Before submitting a pull request, please review our contribution guide

Security

Please consult the security guide for our responsible security vulnerability disclosure process

License

Copyright (c) 2024 Oracle and/or its affiliates and released under the Universal Permissive License (UPL), Version 1.0.

Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.