Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support put device token to life cycle events #144

Closed
ghost opened this issue Mar 17, 2020 · 5 comments
Closed

Support put device token to life cycle events #144

ghost opened this issue Mar 17, 2020 · 5 comments
Labels
enhancement New feature or request

Comments

@ghost
Copy link

ghost commented Mar 17, 2020

Hi guys, My react native app need to implement push notifications that required put device token to segment life cycle events. Does segment react-native lib support to add device token to life cycle events?

Something like this: https://segment.com/docs/connections/sources/catalog/libraries/mobile/ios/#how-do-i-use-push-notifications
https://segment.com/docs/connections/destinations/catalog/customer-io/#device-token-set-up

@hvlong
Copy link

hvlong commented Mar 17, 2020

@quyentran-agilityio I have the same issue.

@adamjuhasz
Copy link

The iOS native library auto-adds it, we use this middleware for android

import {
  Notification,
  Notifications,
  Registered,
} from 'react-native-notifications';

// global to store token
let deviceToken: string | null = null;

// called at startup
export const registerforPN = () => {

  if (Platform.OS === 'android') {
    Notifications.registerRemoteNotifications();
    Notifications.events().registerRemoteNotificationsRegistered(
      (event: Registered) => {
        deviceToken = event.deviceToken;
      },
    );
  }
};

// middleware for this library
export const pnMiddleware: Middleware = payload => {
  const {next, context} = payload;
  if (Platform.OS === 'android' && deviceToken !== null) {
    if (!context.device) {
      context.device = {token: deviceToken};
    }
  }
  next(context);
};

@bsneed
Copy link
Contributor

bsneed commented Apr 13, 2020

@adamjuhasz that is a VERY clever work around, I dig it. We're gonna make this easier though.

@bsneed bsneed added the enhancement New feature or request label Apr 13, 2020
@bsneed bsneed closed this as completed Jan 19, 2021
@aliwaqar981
Copy link

@adamjuhasz

The iOS native library auto-adds it, we use this middleware for android

import {
  Notification,
  Notifications,
  Registered,
} from 'react-native-notifications';

// global to store token
let deviceToken: string | null = null;

// called at startup
export const registerforPN = () => {

  if (Platform.OS === 'android') {
    Notifications.registerRemoteNotifications();
    Notifications.events().registerRemoteNotificationsRegistered(
      (event: Registered) => {
        deviceToken = event.deviceToken;
      },
    );
  }
};

// middleware for this library
export const pnMiddleware: Middleware = payload => {
  const {next, context} = payload;
  if (Platform.OS === 'android' && deviceToken !== null) {
    if (!context.device) {
      context.device = {token: deviceToken};
    }
  }
  next(context);
};

This is only working for custom events. Is there any wayout to set data in context for default lifecycle events?

@cdedreuille
Copy link

I'm having a same issue. In our set up we have trackAppLifecycleEvents: true set up when creating the client. I'm not sure how I could add the middleware on the default lifecycles events.

Any idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants