mirror of
https://github.com/zhigang1992/react-native-notifications.git
synced 2026-06-10 07:19:38 +08:00
113 lines
16 KiB
HTML
113 lines
16 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charSet="utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><title>React Native Notifications Getting Started Guide · React native notifications</title><meta name="viewport" content="width=device-width"/><meta name="generator" content="Docusaurus"/><meta name="description" content="Currently this guide is written for `react-native-notifications@^3.0.0`. "/><meta name="docsearch:language" content="en"/><meta property="og:title" content="React Native Notifications Getting Started Guide · React native notifications"/><meta property="og:type" content="website"/><meta property="og:url" content="https://wix.github.io/react-native-notifications/"/><meta property="og:description" content="Currently this guide is written for `react-native-notifications@^3.0.0`. "/><meta property="og:image" content="https://wix.github.io/react-native-notifications/img/undraw_online.svg"/><meta name="twitter:card" content="summary"/><meta name="twitter:image" content="https://wix.github.io/react-native-notifications/img/undraw_tweetstorm.svg"/><link rel="shortcut icon" href="/react-native-notifications/undefined"/><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css"/><script type="text/javascript" src="https://buttons.github.io/buttons.js"></script><script src="/react-native-notifications/js/scrollSpy.js"></script><link rel="stylesheet" href="/react-native-notifications/css/main.css"/><script src="/react-native-notifications/js/codetabs.js"></script></head><body class="sideNavVisible separateOnPageNav"><div class="fixedHeaderContainer"><div class="headerWrapper wrapper"><header><a href="/react-native-notifications/"><h2 class="headerTitle">React native notifications</h2></a><div class="navigationWrapper navigationSlider"><nav class="slidingNav"><ul class="nav-site nav-site-internal"><li class="siteNavGroupActive"><a href="/react-native-notifications/docs/installation-ios" target="_self">Docs</a></li><li class=""><a href="/react-native-notifications/docs/general-api" target="_self">API</a></li></ul></nav></div></header></div></div><div class="navPusher"><div class="docMainWrapper wrapper"><div class="docsNavContainer" id="docsNav"><nav class="toc"><div class="toggleNav"><section class="navWrapper wrapper"><div class="navBreadcrumb wrapper"><div class="navToggle" id="navToggler"><div class="hamburger-menu"><div class="line1"></div><div class="line2"></div><div class="line3"></div></div></div><h2><i>›</i><span>Guides</span></h2><div class="tocToggler" id="tocToggler"><i class="icon-toc"></i></div></div><div class="navGroups"><div class="navGroup"><h3 class="navGroupCategoryTitle">Guides</h3><ul class=""><li class="navListItem navListItemActive"><a class="navItem" href="/react-native-notifications/docs/getting-started">Getting Started</a></li><li class="navListItem"><a class="navItem" href="/react-native-notifications/docs/subscription">Subscription</a></li><li class="navListItem"><a class="navItem" href="/react-native-notifications/docs/notifications-events">Events</a></li><li class="navListItem"><a class="navItem" href="/react-native-notifications/docs/localNotifications">Local Notifications</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Installation</h3><ul class=""><li class="navListItem"><a class="navItem" href="/react-native-notifications/docs/installation-ios">iOS Installation</a></li><li class="navListItem"><a class="navItem" href="/react-native-notifications/docs/installation-android">Android Installation</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Advanced</h3><ul class=""><li class="navListItem"><a class="navItem" href="/react-native-notifications/docs/advanced-ios">iOS</a></li></ul></div></div></section></div><script>
|
||
var coll = document.getElementsByClassName('collapsible');
|
||
var checkActiveCategory = true;
|
||
for (var i = 0; i < coll.length; i++) {
|
||
var links = coll[i].nextElementSibling.getElementsByTagName('*');
|
||
if (checkActiveCategory){
|
||
for (var j = 0; j < links.length; j++) {
|
||
if (links[j].classList.contains('navListItemActive')){
|
||
coll[i].nextElementSibling.classList.toggle('hide');
|
||
coll[i].childNodes[1].classList.toggle('rotate');
|
||
checkActiveCategory = false;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
coll[i].addEventListener('click', function() {
|
||
var arrow = this.childNodes[1];
|
||
arrow.classList.toggle('rotate');
|
||
var content = this.nextElementSibling;
|
||
content.classList.toggle('hide');
|
||
});
|
||
}
|
||
|
||
document.addEventListener('DOMContentLoaded', function() {
|
||
createToggler('#navToggler', '#docsNav', 'docsSliderActive');
|
||
createToggler('#tocToggler', 'body', 'tocActive');
|
||
|
||
var headings = document.querySelector('.toc-headings');
|
||
headings && headings.addEventListener('click', function(event) {
|
||
var el = event.target;
|
||
while(el !== headings){
|
||
if (el.tagName === 'A') {
|
||
document.body.classList.remove('tocActive');
|
||
break;
|
||
} else{
|
||
el = el.parentNode;
|
||
}
|
||
}
|
||
}, false);
|
||
|
||
function createToggler(togglerSelector, targetSelector, className) {
|
||
var toggler = document.querySelector(togglerSelector);
|
||
var target = document.querySelector(targetSelector);
|
||
|
||
if (!toggler) {
|
||
return;
|
||
}
|
||
|
||
toggler.onclick = function(event) {
|
||
event.preventDefault();
|
||
|
||
target.classList.toggle(className);
|
||
};
|
||
}
|
||
});
|
||
</script></nav></div><div class="container mainContainer docsContainer"><div class="wrapper"><div class="post"><header class="postHeader"><h1 id="__docusaurus" class="postHeaderTitle">React Native Notifications Getting Started Guide</h1></header><article><div><span><p>Currently this guide is written for <code>react-native-notifications@^3.0.0</code>.<br>
|
||
We also assume you use <code>react-native@60.x.x</code> and above.</p>
|
||
<p>For older versions, visit this <a href="https://github.com/wix/react-native-notifications/blob/v2/docs/installation.md">installation guide</a>.</p>
|
||
<h2><a class="anchor" aria-hidden="true" id="add-react-native-notifications-to-your-dependencies"></a><a href="#add-react-native-notifications-to-your-dependencies" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Add react-native-notifications to your dependencies</h2>
|
||
<h4><a class="anchor" aria-hidden="true" id="with-npm"></a><a href="#with-npm" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>With npm</h4>
|
||
<pre><code class="hljs">$ <span class="hljs-built_in">npm</span> install --save react-<span class="hljs-keyword">native</span>-notifications
|
||
</code></pre>
|
||
<h4><a class="anchor" aria-hidden="true" id="or-with-yarn"></a><a href="#or-with-yarn" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Or with yarn</h4>
|
||
<pre><code class="hljs">$ yarn <span class="hljs-keyword">add</span><span class="bash"> react-native-notifications</span>
|
||
</code></pre>
|
||
<h2><a class="anchor" aria-hidden="true" id="link-native-dependencies"></a><a href="#link-native-dependencies" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Link native dependencies</h2>
|
||
<h3><a class="anchor" aria-hidden="true" id="ios"></a><a href="#ios" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>iOS</h3>
|
||
<pre><code class="hljs">$ pod <span class="hljs-keyword">install</span> --<span class="hljs-keyword">project</span>-directory=ios/
|
||
</code></pre>
|
||
<p>Start monitor notifications in <code>AppDelegate.m</code>:</p>
|
||
<pre><code class="hljs css language-objective-c">
|
||
- (<span class="hljs-built_in">BOOL</span>)application:(<span class="hljs-built_in">UIApplication</span> *)application didFinishLaunchingWithOptions:(<span class="hljs-built_in">NSDictionary</span> *)launchOptions {
|
||
[RNNotifications startMonitorNotifications]; <span class="hljs-comment">// -> Add this line</span>
|
||
|
||
<span class="hljs-keyword">return</span> <span class="hljs-literal">YES</span>;
|
||
}
|
||
|
||
</code></pre>
|
||
<p>And add the following methods to support registration to <code>AppDelegate.m</code>:</p>
|
||
<pre><code class="hljs css language-objective-c">
|
||
- (<span class="hljs-keyword">void</span>)<span class="hljs-string">application:</span>(UIApplication *)application <span class="hljs-string">didRegisterForRemoteNotificationsWithDeviceToken:</span>(NSData *)deviceToken {
|
||
[RNNotifications <span class="hljs-string">didRegisterForRemoteNotificationsWithDeviceToken:</span>deviceToken];
|
||
}
|
||
|
||
- (<span class="hljs-keyword">void</span>)<span class="hljs-string">application:</span>(UIApplication *)application <span class="hljs-string">didFailToRegisterForRemoteNotificationsWithError:</span>(NSError *)error {
|
||
[RNNotifications <span class="hljs-string">didFailToRegisterForRemoteNotificationsWithError:</span>error];
|
||
}
|
||
</code></pre>
|
||
<h3><a class="anchor" aria-hidden="true" id="android"></a><a href="#android" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Android</h3>
|
||
<p>For Android installation, please refer to the <a href="/react-native-notifications/docs/installation-android">Android installation doc</a> where you can find detailed step on how to start using Google's FCM service.</p>
|
||
<h2><a class="anchor" aria-hidden="true" id="register-for-notification-events"></a><a href="#register-for-notification-events" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Register for notification events</h2>
|
||
<pre><code class="hljs css language-js"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
|
||
<span class="hljs-keyword">import</span> {Notifications} <span class="hljs-keyword">from</span> <span class="hljs-string">'react-native-notifications'</span>;
|
||
|
||
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyComponent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
|
||
<span class="hljs-keyword">constructor</span>() {
|
||
Notifications.registerRemoteNotifications();
|
||
|
||
Notifications.events().registerNotificationReceived(<span class="hljs-function">(<span class="hljs-params">notification: Notification, completion</span>) =></span> {
|
||
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Notification received in foreground: <span class="hljs-subst">${notification.title}</span> : <span class="hljs-subst">${notification.body}</span>`</span>);
|
||
completion({<span class="hljs-attr">alert</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">sound</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">badge</span>: <span class="hljs-literal">false</span>});
|
||
});
|
||
|
||
Notifications.events().registerRemoteNotificationOpened(<span class="hljs-function">(<span class="hljs-params">notification: Notification, completion</span>) =></span> {
|
||
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Notification opened: <span class="hljs-subst">${notification.payload}</span>`</span>);
|
||
completion();
|
||
});
|
||
}
|
||
}
|
||
</code></pre>
|
||
<p>Next, check out the <a href="/react-native-notifications/docs/general-api">API Reference</a>.</p>
|
||
</span></div></article></div><div class="docs-prevnext"><a class="docs-next button" href="/react-native-notifications/docs/subscription"><span>Subscription</span><span class="arrow-next"> →</span></a></div></div></div><nav class="onPageNav"><ul class="toc-headings"><li><a href="#add-react-native-notifications-to-your-dependencies">Add react-native-notifications to your dependencies</a></li><li><a href="#link-native-dependencies">Link native dependencies</a><ul class="toc-headings"><li><a href="#ios">iOS</a></li><li><a href="#android">Android</a></li></ul></li><li><a href="#register-for-notification-events">Register for notification events</a></li></ul></nav></div><footer class="nav-footer" id="footer"><section class="sitemap"><a href="/react-native-notifications/" class="nav-home"></a><div><h5>Docs</h5><a href="/react-native-notifications/docs/getting-started">Getting Started (or other categories)</a><a href="/react-native-notifications/docs/installation-ios">Guides</a><a href="/react-native-notifications/docs/general-api">API Reference</a></div><div></div><div><h5>More</h5><a href="/react-native-notifications/blog">Blog</a><a href="https://github.com/wix/react-native-notifications">GitHub</a><a class="github-button" href="https://github.com/wix/react-native-notifications" data-icon="octicon-star" data-count-href="/facebook/docusaurus/stargazers" data-show-count="true" data-count-aria-label="# stargazers on GitHub" aria-label="Star this project on GitHub">Star</a></div></section><section class="copyright">Copyright © 2020 Wix</section></footer></div></body></html> |