Skip to content

Release 3.0.0

Compare
Choose a tag to compare
@zxcpoiu zxcpoiu released this 08 Dec 08:23
· 20 commits to master since this release

Breaking Change:

Removal and Deprecation

remove unused codes, keep this lib simple, lots functionalities should be easily implemented in the modern RN eco system

  • remove requestPermission ( should use PushNotificationIOS instead if needed )
  • remove wakeupByPush constant with push event ( should use AppState instead if needed )

Changes:

  • use NativeEventEmitter instead the deprecated DeviceEventEmitter
  • tweak event handler logic
  • support caching events before js initialize
    you can subscribe didLoadWithEvents when app ready

Upgrade Guide:

  1. remove deprecation functions or use corresponded 3rd party library to implement it
  2. Follow the new doc in README to tweak your logic

Mainly, you just have to subscribe a new event didLoadWithEvent, which helps you to catch the initial event before JS bridge initialed.
This event

NOTE: You still need to subscribe / handle the rest events as usuall. This is just a helper whcih cache and propagate early fired events if and only if for "the native events which DID fire BEFORE js bridge is initialed", it does NOT mean this will have events each time when the app reopened.

For the discussion, see #69

Doc copied from the readme at the time of writing below

API and Usage:

Native API:

Voip Push is time sensitive, these native API mainly used in AppDelegate.m, especially before JS bridge is up.
This usually

  • (void)voipRegistration ---
    register delegate for PushKit if you like to register in AppDelegate.m ASAP instead JS side ( too late for some use cases )
  • (void)didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type ---
    call this api to fire 'register' event to JS
  • (void)didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type ---
    call this api to fire 'notification' event to JS
  • (void)addCompletionHandler:(NSString *)uuid completionHandler:(RNVoipPushNotificationCompletion)completionHandler ---
    add completionHandler to RNVoipPush module
  • (void)removeCompletionHandler:(NSString *)uuid ---
    remove completionHandler to RNVoipPush module

JS API:

  • registerVoipToken() --- JS method to register PushKit delegate
  • onVoipNotificationCompleted(notification.uuid) --- JS mehtod to tell PushKit we have handled received voip push

Events:

  • 'register' --- fired when PushKit give us the latest token
  • 'notification' --- fired when received voip push notification
  • 'didLoadWithEvents' --- fired when there are not-fired events been cached before js bridge is up

JS usage

...

import VoipPushNotification from 'react-native-voip-push-notification';

...

class MyComponent extends React.Component {

...

    // --- anywhere which is most comfortable and appropriate for you,
    // --- usually ASAP, ex: in your app.js or at some global scope.
    componentDidMount() {

        // --- NOTE: You still need to subscribe / handle the rest events as usuall.
        // --- This is just a helper whcih cache and propagate early fired events if and only if for
        // --- "the native events which DID fire BEFORE js bridge is initialed",
        // --- it does NOT mean this will have events each time when the app reopened.


        // ===== Step 1: subscribe `register` event =====
        // --- this.onVoipPushNotificationRegistered
        VoipPushNotification.addEventListener('register', (token) => {
            // --- send token to your apn provider server
        });

        // ===== Step 2: subscribe `notification` event =====
        // --- this.onVoipPushNotificationiReceived
        VoipPushNotification.addEventListener('notification', (notification) => {
            // --- when receive remote voip push, register your VoIP client, show local notification ... etc
            this.doSomething();
          
            // --- optionally, if you `addCompletionHandler` from the native side, once you have done the js jobs to initiate a call, call `completion()`
            VoipPushNotification.onVoipNotificationCompleted(notification.uuid);
        });

        // ===== Step 3: subscribe `didLoadWithEvents` event =====
        VoipPushNotification.addEventListener('didLoadWithEvents', (events) => {
            // --- this will fire when there are events occured before js bridge initialized
            // --- use this event to execute your event handler manually by event type

            if (!events || !Array.isArray(events) || events.length < 1) {
                return;
            }
            for (let voipPushEvent of events) {
                let { name, data } = voipPushEvent;
                if (name === VoipPushNotification.RNVoipPushRemoteNotificationsRegisteredEvent) {
                    this.onVoipPushNotificationRegistered(data);
                } else if (name === VoipPushNotification.RNVoipPushRemoteNotificationReceivedEvent) {
                    this.onVoipPushNotificationiReceived(data);
                }
            }
        });

        // ===== Step 4: register =====
        // --- it will be no-op ( no event will be fired ) if you have subscribed before like in native side.
        VoipPushNotification.registerVoipToken(); // --- register token
    }

    // --- unsubscribe event listeners
    componentWillUnmount() {
        VoipPushNotification.removeEventListener('didLoadWithEvents');
        VoipPushNotification.removeEventListener('register');
        VoipPushNotification.removeEventListener('notification');
    }
...
}

AppDelegate.m Modification Optional Change

You can now register token ASAP in your AppDelegate.m if you want
This is optional but recommended way

...

#import <PushKit/PushKit.h>                    /* <------ add this line */
#import "RNVoipPushNotificationManager.h"      /* <------ add this line */

...

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  


  // ===== (THIS IS OPTIONAL BUT RECOMMENDED) =====
  // --- register VoipPushNotification here ASAP rather than in JS. Doing this from the JS side may be too slow for some use cases
  // --- see: https://github.com/react-native-webrtc/react-native-voip-push-notification/issues/59#issuecomment-691685841
  [RNVoipPushNotificationManager voipRegistration];
  // ===== (THIS IS OPTIONAL BUT RECOMMENDED) =====



  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"AppName" initialProperties:nil];
}