Giter VIP home page Giter VIP logo

react-native-in-app-notification's Introduction

react-native-in-app-notification npm version

🔔 Customisable in-app notification component for React Native

Contents

  1. UI
  2. Install
  3. Versions
  4. Props
  5. Usage

UI

The basic look of react-native-in-app-notification:

A GIF showing what react-native-in-app-notification can do

What you can make react-native-in-app-notification do with a customised component:

A GIF showing what react-native-in-app-notification can do

Install

yarn add react-native-in-app-notification

OR

npm install react-native-in-app-notification --save

Android

For Android you need to add the VIBRATE permission to your app AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="your.app.package.name">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

    <!-- Required by react-native-in-app-notification -->
    <uses-permission android:name="android.permission.VIBRATE" />

    ...
</manifest>

Versions

version RN
>=3.0.0 >= 0.54.0
<3.0.0 >= 0.43.4

Props

Prop Name Prop Description Data Type Required Default
closeInterval How long the notification stays visible Number No 4000
openCloseDuration The length of the open / close animation Number No 200
height The height of the Notification component Number No 80
backgroundColour The background colour of the Notification component String No white
iconApp App Icon ImageSourcePropType No null
notificationBodyComponent See below about NotificationBody React Node or Function Recommended ./DefaultNotificationBody

NotificationBody

The notification body is what is rendered inside the main Notification component and gives you the ability to customise how the notification looks. You can use the default notification body component in ./DefaultNotificationBody.js as inspiration and guidance.

Your notificationBodyComponent component is given five props:

Prop Name Prop Description Data Type Default
title The title passed to NotificationRef.show String ''
message The message passed to NotificationRef.show String ''
onPress The callback passed to NotificationRef.show Function null
icon Icon for notification passed to NotificationRef.show ImageSourcePropType null
vibrate Vibrate on show notification passed to NotificationRef.show Boolean true
additionalProps Any additional props passed to NotificationBodyComponent Object null

Usage

Adding react-native-in-app-notification is simple; Just wrap you main App component with the InAppNotificationProvider component exported from this module.

import { InAppNotificationProvider } from 'react-native-in-app-notification';

import App from './App.js';

class AppWithNotifications extends Component {
  render() {
    return (
      <InAppNotificationProvider>
        <App />
      </InAppNotificationProvider>
    );
  }
}

When you want to show the notification, just wrap the component which needs to display a notification with the withInAppNotification HOC and call the .showNotification methods from its props.

.showNotification can take four arguments (all of which are optional):

  • title
  • message
  • onPress
  • additionalProps

N.B: you should probably include at least one of title or message!

onPress doesn't need to be used for passive notifications and you can use onClose in your NotificationBody component to allow your users to close the notification.

additionalProps can be used to pass arbitory props to NotificationBody component. Can be accessed in NotificationBody component via props.additionalProps.

import React, { Component } from 'react';
import { View, Text, TouchableHighlight } from 'react-native';
import { withInAppNotification } from 'react-native-in-app-notification';

class MyApp extends Component {
  render() {
    return (
      <View>
        <Text>This is my app</Text>
        <TouchableHighlight
          onPress={() => {
            this.props.showNotification({
              title: 'You pressed it!',
              message: 'The notification has been triggered',
              onPress: () => Alert.alert('Alert', 'You clicked the notification!'),
              additionalProps: { type: 'error' },
            });
          }}
        >
          <Text>Click me to trigger a notification</Text>
        </TouchableHighlight>
      </View>
    );
  }
}

export default withInAppNotification(MyApp);

react-native-in-app-notification's People

Contributors

dependabot[bot] avatar larsandremoen avatar luizeduard0 avatar renatomserra avatar robcalcroft avatar timothystewart6 avatar tofoli avatar vitaliik91 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

react-native-in-app-notification's Issues

Push to npm

Is it possible to push the rewrite to npm registry?

Redux support

Hi, can you please show me an example of using react-native-in-app-notification with react-redux.

On your docs says:

export default withInAppNotification(MyApp);

But i need to use it on my Routes file (so i can routing on notification onPress event), i am using react-native-router-flux, so right now i have this on my Routes file:

export default connect(mapStateToProps)(Routes);

I think i can't consume the showNotification prop on that file.

Maybe if i use this approach can i make it?:

https://gist.github.com/testshallpass/d76c656874e417bef4e0e6a63fc492af

So not using the context and use the static class helper.

What do you think guys.

Thanks.

consecutive calls shows just first one

Problem

if i call the notification two or more times consecutively it renders just the first one. If i use a timeout for the second one i get a good behaviour.

Proposal

I'm thinking implement a delayed queue in order to show consecutive calls in a queued way and show them one by one with a timeout. Do you accept a pull request for this?

DefaultNotificationBody.js would be better if the changes are done

  1. onPress is not at the outmost layer that overwrites onClose
  2. Could be more clear that onClose is a predefined function, not a default value that you can pass into the custom component props.
  <TouchableHighlight
    style={defaultStyles.root}
    activeOpacity={0.3}
    underlayColor="transparent"
  >
    <View style={[defaultStyles.container, { paddingBottom: title && message ? 10 : 0 }]}>
      <TouchableHighlight
        style={defaultStyles.root}
        activeOpacity={0.3}
        underlayColor="transparent"
        onPress={() => {
          onPress()
          onClose()
        }}
      >
        <View>
          {title ? <Text style={[defaultStyles.text, defaultStyles.bold]}>{title}</Text> : null}
          {message ? <Text style={defaultStyles.text}>{message}</Text> : null}
        </View>
      </TouchableHighlight>
      <View>
        <TouchableHighlight activeOpacity={0.3} underlayColor="transparent" onPress={onClose}>
          <Text>Close</Text>
        </TouchableHighlight>
      </View>
    </View>
  </TouchableHighlight>

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on Greenkeeper branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please click the 'fix repo' button on account.greenkeeper.io.

Icon example usage

Hey there,

I am trying to pass an icon through but cant get it to display, here is a snippet of my code:

{this.notification && this.notification.show({
title: 'Notification (Tap to close)',
message: 'Hello World',
icon: 'http://www.icon100.com/up/3708/128/630.png',
onPress: () => this.notification.closeNotification()
})}

What am I doing wrong?

Customize Notification Background

Hi,
firstly, this lib is awesome eh. Great job!
this is not a issue per say,

I'd like to understand how can I use background props. How can I customize the notification. I couldn't find any tutorial or documentation.

Disable vibration not working on ios

Unable to disable the vibration with vibrate=false.

For me it looks like it's becuase this guard is always true due to prevProps.vibrate always being true.

Modifying the guard to the following seems to be working as intended:
if (this.props.vibrate && this.props.isOpen && !prevProps.isOpen) {...}

Screen content shifts when notification appears during ios in-call status

A standard iOS status bar is 20px in height, so most apps have a 20px top spacer to compensate.

When the iOS in-call status is active, the status bar is effectively 40px in height, but iOS just shrinks the entire viewport to make room for the additional 20px. So the application doesn't have to, for example, increase its padding to 40px.

When react-native-in-app-notification hides the status bar for a few seconds while showing an alert, the shrunken viewport expands by 20px back to its default.

This causes the entire layout to shift when a notification is received, and then shift back when it disappears. This only occurs when the in-call status is active.

closeInterval not working

Tried to use closeInterval property but not working and changing default in source also makes no change.

Image prop

This is not a issue per say but how can i add image to shownotifcation.
i have the follow code below however the image is not showing
this.props.showNotification({
title: 'Update Received!',
message: data.message,
onPress: () => this.props.navigation.navigate('update',{messagedata:data.message}),
icon: <Image source={{uri:'ttps://facebook.github.io/react-native/docs/assets/favicon.png'}} />
});

Notification background disappears couple seconds after notification

In the following code, white background stays in view for couple seconds after notification disappears. I've seen this in Android but not tested on iOS yet.

Is it a bug or am I missing something?

`import React from "react";
import {View, Platform, PushNotificationIOS, StyleSheet} from "react-native";
import Notification from "react-native-in-app-notification";

const defaultIcon = require("../assets/defaultImages/logo_small.png");

export class InAppNotification extends React.Component {

handleRegister = token => {
    console.log('push token', token);
};

handleNotification = notification => {
    console.log("notification", notification);
    const message = Platform.OS === "ios" ? notification._alert : notification;
    if (!message) {
        console.log("Empty message.");
    } else {
        console.log("message", message);
        if (!this.notification) {
            console.log("In app notification ref is not set yet.");
        } else {
            this.notification.show({
                icon: defaultIcon,
                title: message.title,
                message: message.body
                // onPress: () => Alert.alert('Alert', 'You clicked the notification!'),
            });
        }
    }

    // required on iOS only (see fetchCompletionHandler docs: https://facebook.github.io/react-native/docs/pushnotificationios.html)
    if (Platform.OS === "ios") {
        notification.finish(PushNotificationIOS.FetchResult.NoData);
    }
};

componentDidMount() {
    const {pushNotification: PushNotification} = this.props;
    PushNotification.onNotification(this.handleNotification);
    PushNotification.onRegister(this.handleRegister);
}

render() {
    return (
        <View style={styles.root}>
            <Notification ref={ref => {this.notification = ref;}}/>
        </View>
    )
}

}

const styles = {
root: {
position: "absolute",
top: 0,
left: 0,
right: 0,
height: 200,
elevation: 999,
zIndex: 999
}
};`

InAppNotificationProvider show by default a notification

Hi,

When i wrap my application with the Provider, i have a notification showed by default.
This is due to the default props of the DefaultNotificationBody:

DefaultNotificationBody.defaultProps = {
  title: 'Notification',
  message: 'This is a test notification',
  vibrate: true,
  isOpen: false,
  iconApp: null,
  icon: null,
  onPress: () => null,
  onClose: () => null,
};

Has to be:

DefaultNotificationBody.defaultProps = {
  title: null,
  message: null,
  vibrate: true,
  isOpen: false,
  iconApp: null,
  icon: null,
  onPress: () => null,
  onClose: () => null,
};

This is happening with expo and android. But visibly same problem with ios.

Thx.

UseNativeDriver

Hi, is there a way to set "useNativeDriver" on notification animation, thanks.

Close notification

My apologies for not having found my answer but how do you close the notification ?
Thank you very much for the great work, and thank you in advance for any help here :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.