Skip to main content

Listen to ROOK SDK notifications

The subscription API depends on the installed ROOK major version:

SDK trackSubscription API
4.x Native Modules for React Native 0.73NativeEventEmitter and getRookModule()
5.x TurboModules for React Native 0.83RookSdk.onRookMessage()

Use the example that matches the version pinned in the application's package.json.

Notification payload​

Both tracks deliver a message with the same shape:

type RookMessage = {
type: string;
value: boolean;
message?: string;
};

Common notification types include:

  • ROOK_BACKGROUND_ANDROID_PERMISSIONS: whether the Android background permissions are granted.
  • ROOK_BACKGROUND_ANDROID_PERMISSIONS_DIALOG_DISPLAYED: whether the permission dialog was displayed.
  • ROOK_HEALTH_CONNECT_PERMISSIONS: whether Health Connect permissions are granted.
  • ROOK_HEALTH_CONNECT_PERMISSIONS_PARTIALLY_GRANTED: whether only some Health Connect permissions are granted.
  • ROOK_HEALTH_CONNECT_BACKGROUND_PERMISSIONS: whether Health Connect background read permission is granted.
  • ROOK_SAMSUNG_HEALTH_PERMISSIONS: whether Samsung Health permissions are granted.
  • ROOK_SAMSUNG_HEALTH_PERMISSIONS_PARTIALLY_GRANTED: whether only some Samsung Health permissions are granted.
  • ROOK_BACKGROUND_ENABLED: whether ROOK background synchronization was enabled successfully.

SDK 5.x: TurboModule listener​

Version 5.x exposes a typed event directly from the generated TurboModule. Do not create a NativeEventEmitter for this version.

import React, { useEffect } from "react";
import { RookSdk } from "react-native-rook-sdk";

export function RookNotificationListener() {
useEffect(() => {
const subscription = RookSdk.onRookMessage(({ type, value, message }) => {
switch (type) {
case "ROOK_HEALTH_CONNECT_PERMISSIONS":
console.log(`Health Connect permissions granted: ${value}`);
break;
case "ROOK_SAMSUNG_HEALTH_PERMISSIONS":
console.log(`Samsung Health permissions granted: ${value}`);
break;
case "ROOK_BACKGROUND_ENABLED":
console.log(`Background synchronization enabled: ${value}`);
break;
default:
console.log(type, value, message);
}
});

return () => subscription.remove();
}, []);

return null;
}

SDK 4.x: Native Modules listener​

Version 4.x exposes the native module with getRookModule(). Subscribe to its ROOK_NOTIFICATION event through React Native's NativeEventEmitter.

import React, { useEffect } from "react";
import { NativeEventEmitter } from "react-native";
import { getRookModule } from "react-native-rook-sdk";

export function RookNotificationListener() {
useEffect(() => {
const rookModule = getRookModule();
const eventEmitter = new NativeEventEmitter(rookModule);

const subscription = eventEmitter.addListener(
"ROOK_NOTIFICATION",
({ type, value, message }) => {
switch (type) {
case "ROOK_HEALTH_CONNECT_PERMISSIONS":
console.log(`Health Connect permissions granted: ${value}`);
break;
case "ROOK_SAMSUNG_HEALTH_PERMISSIONS":
console.log(`Samsung Health permissions granted: ${value}`);
break;
case "ROOK_BACKGROUND_ENABLED":
console.log(`Background synchronization enabled: ${value}`);
break;
default:
console.log(type, value, message);
}
}
);

return () => subscription.remove();
}, []);

return null;
}

Always remove the subscription when the component unmounts. This prevents duplicate callbacks after navigation or development hot reloads.

Background synchronization events

To receive ROOK_BACKGROUND_ENABLED, enable background synchronization in RookSyncGate after obtaining the user's consent.

When migrating the listener from 4.x to 5.x, remove both getRookModule() and NativeEventEmitter; the event name is represented by the typed onRookMessage property instead. See the 4.x to 5.x migration guide for the remaining changes.