Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 28d368a

Browse files
committed
First example app (Only on Android, tested only in emulator)
1 parent 91d8c2e commit 28d368a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+13256
-3091
lines changed

example/.buckconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
[android]
3+
target = Google Inc.:Google APIs:23
4+
5+
[maven_repositories]
6+
central = https://repo1.maven.org/maven2

example/.flowconfig

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
[ignore]
2+
; We fork some components by platform
3+
.*/*[.]android.js
4+
5+
; Ignore "BUCK" generated dirs
6+
<PROJECT_ROOT>/\.buckd/
7+
8+
; Ignore unexpected extra "@providesModule"
9+
.*/node_modules/.*/node_modules/fbjs/.*
10+
11+
; Ignore duplicate module providers
12+
; For RN Apps installed via npm, "Libraries" folder is inside
13+
; "node_modules/react-native" but in the source repo it is in the root
14+
.*/Libraries/react-native/React.js
15+
16+
; Ignore polyfills
17+
.*/Libraries/polyfills/.*
18+
19+
; Ignore metro
20+
.*/node_modules/metro/.*
21+
22+
[include]
23+
24+
[libs]
25+
node_modules/react-native/Libraries/react-native/react-native-interface.js
26+
node_modules/react-native/flow/
27+
node_modules/react-native/flow-github/
28+
29+
[options]
30+
emoji=true
31+
32+
module.system=haste
33+
module.system.haste.use_name_reducers=true
34+
# get basename
35+
module.system.haste.name_reducers='^.*/\([a-zA-Z0-9$_.-]+\.js\(\.flow\)?\)$' -> '\1'
36+
# strip .js or .js.flow suffix
37+
module.system.haste.name_reducers='^\(.*\)\.js\(\.flow\)?$' -> '\1'
38+
# strip .ios suffix
39+
module.system.haste.name_reducers='^\(.*\)\.ios$' -> '\1'
40+
module.system.haste.name_reducers='^\(.*\)\.android$' -> '\1'
41+
module.system.haste.name_reducers='^\(.*\)\.native$' -> '\1'
42+
module.system.haste.paths.blacklist=.*/__tests__/.*
43+
module.system.haste.paths.blacklist=.*/__mocks__/.*
44+
module.system.haste.paths.blacklist=<PROJECT_ROOT>/node_modules/react-native/Libraries/Animated/src/polyfills/.*
45+
module.system.haste.paths.whitelist=<PROJECT_ROOT>/node_modules/react-native/Libraries/.*
46+
47+
munge_underscores=true
48+
49+
module.name_mapper='^[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> 'RelativeImageStub'
50+
51+
module.file_ext=.js
52+
module.file_ext=.jsx
53+
module.file_ext=.json
54+
module.file_ext=.native.js
55+
56+
suppress_type=$FlowIssue
57+
suppress_type=$FlowFixMe
58+
suppress_type=$FlowFixMeProps
59+
suppress_type=$FlowFixMeState
60+
61+
suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe\\($\\|[^(]\\|(\\(<VERSION>\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)
62+
suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(<VERSION>\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)?:? #[0-9]+
63+
suppress_comment=\\(.\\|\n\\)*\\$FlowFixedInNextDeploy
64+
suppress_comment=\\(.\\|\n\\)*\\$FlowExpectedError
65+
66+
[version]
67+
^0.75.0

example/.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pbxproj -text

example/.gitignore

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,56 @@
1-
# See https://help.github.com/ignore-files/ for more about ignoring files.
1+
# OSX
2+
#
3+
.DS_Store
24

3-
# expo
4-
.expo/
5+
# Xcode
6+
#
7+
build/
8+
*.pbxuser
9+
!default.pbxuser
10+
*.mode1v3
11+
!default.mode1v3
12+
*.mode2v3
13+
!default.mode2v3
14+
*.perspectivev3
15+
!default.perspectivev3
16+
xcuserdata
17+
*.xccheckout
18+
*.moved-aside
19+
DerivedData
20+
*.hmap
21+
*.ipa
22+
*.xcuserstate
23+
project.xcworkspace
524

6-
# dependencies
7-
/node_modules
25+
# Android/IntelliJ
26+
#
27+
build/
28+
.idea
29+
.gradle
30+
local.properties
31+
*.iml
832

9-
# misc
10-
.env.local
11-
.env.development.local
12-
.env.test.local
13-
.env.production.local
33+
# node.js
34+
#
35+
node_modules/
36+
npm-debug.log
37+
yarn-error.log
1438

15-
npm-debug.log*
16-
yarn-debug.log*
17-
yarn-error.log*
39+
# BUCK
40+
buck-out/
41+
\.buckd/
42+
*.keystore
43+
44+
# fastlane
45+
#
46+
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
47+
# screenshots whenever they are needed.
48+
# For more information about the recommended setup visit:
49+
# https://docs.fastlane.tools/best-practices/source-control/
50+
51+
*/fastlane/report.xml
52+
*/fastlane/Preview.html
53+
*/fastlane/screenshots
54+
55+
# Bundle artifact
56+
*.jsbundle

example/.watchmanconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{}
1+
{}

example/App.js

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,94 @@
1-
import React from 'react';
2-
import { StyleSheet, Text, View } from 'react-native';
1+
/**
2+
* Sample React Native App
3+
* https://github.com/facebook/react-native
4+
*
5+
* @format
6+
* @flow
7+
*/
8+
9+
import React, { Component } from 'react';
10+
import { TextInput, StyleSheet, Text, View, TouchableOpacity, Alert } from 'react-native';
11+
import NotifService from './NotifService';
12+
13+
14+
type Props = {};
15+
export default class App extends Component<Props> {
16+
17+
constructor(props) {
18+
super(props);
19+
this.state = {};
20+
21+
this.notif = new NotifService(this.onRegister.bind(this), this.onNotif.bind(this));
22+
}
323

4-
export default class App extends React.Component {
524
render() {
625
return (
726
<View style={styles.container}>
8-
<Text>Open up App.js to start working on your app!</Text>
9-
<Text>Changes you make will automatically reload.</Text>
10-
<Text>Shake your phone to open the developer menu.</Text>
27+
28+
<Text style={styles.title}>Example app react-native-push-notification</Text>
29+
<View style={styles.spacer}></View>
30+
<TextInput style={styles.textField} value={this.state.registerToken} placeholder="Register token" />
31+
<View style={styles.spacer}></View>
32+
33+
<TouchableOpacity style={styles.button} onPress={() => {this.notif.localNotif()} }><Text>Local Notification (now)</Text></TouchableOpacity>
34+
<TouchableOpacity style={styles.button} onPress={() => {this.notif.scheduleNotif()} }><Text>Schedule Notification in 30s</Text></TouchableOpacity>
35+
<TouchableOpacity style={styles.button} onPress={() => {this.notif.cancelNotif()} }><Text>Cancel last notification (if any)</Text></TouchableOpacity>
36+
<TouchableOpacity style={styles.button} onPress={() => {this.notif.cancelAll()} }><Text>Cancel all notifications</Text></TouchableOpacity>
37+
38+
<TouchableOpacity style={styles.button} onPress={() => {this.notif.checkPermission(this.handlePerm.bind(this))}}><Text>Check Permission</Text></TouchableOpacity>
1139
</View>
1240
);
1341
}
42+
43+
onRegister(token) {
44+
console.log(token);
45+
this.setState({ registerToken: token });
46+
}
47+
48+
onNotif(notif) {
49+
console.log(notif);
50+
Alert.alert(notif.title, notif.message);
51+
}
52+
53+
handlePerm(perms) {
54+
Alert.alert("Permissions", JSON.stringify(perms));
55+
}
1456
}
1557

58+
1659
const styles = StyleSheet.create({
1760
container: {
1861
flex: 1,
19-
backgroundColor: '#fff',
20-
alignItems: 'center',
2162
justifyContent: 'center',
63+
alignItems: 'center',
64+
backgroundColor: '#F5FCFF',
2265
},
66+
welcome: {
67+
fontSize: 20,
68+
textAlign: 'center',
69+
margin: 10,
70+
},
71+
button: {
72+
borderWidth: 1,
73+
borderColor: "#000000",
74+
margin: 5,
75+
padding: 5,
76+
width: "70%",
77+
backgroundColor: "#DDDDDD",
78+
borderRadius: 5,
79+
},
80+
textField: {
81+
borderWidth: 1,
82+
borderColor: "#AAAAAA",
83+
margin: 5,
84+
padding: 5,
85+
width: "70%"
86+
},
87+
spacer: {
88+
height: 10,
89+
},
90+
title: {
91+
fontWeight: "bold",
92+
fontSize: 20,
93+
}
2394
});

example/App.test.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

example/NotifService.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import PushNotification from 'react-native-push-notification';
2+
3+
export default class NotifService {
4+
5+
constructor(onRegister, onNotification) {
6+
this._config(onRegister, onNotification);
7+
8+
this.lastId = 0;
9+
}
10+
11+
_config(onRegister, onNotification, gcm = "") {
12+
PushNotification.configure({
13+
// (optional) Called when Token is generated (iOS and Android)
14+
onRegister: onRegister, //this._onRegister.bind(this),
15+
16+
// (required) Called when a remote or local notification is opened or received
17+
onNotification: onNotification, //this._onNotification,
18+
19+
// ANDROID ONLY: GCM Sender ID (optional - not required for local notifications, but is need to receive remote push notifications)
20+
senderID: gcm,
21+
22+
// IOS ONLY (optional): default: all - Permissions to register.
23+
permissions: {
24+
alert: true,
25+
badge: true,
26+
sound: true
27+
},
28+
29+
// Should the initial notification be popped automatically
30+
// default: true
31+
popInitialNotification: true,
32+
33+
/**
34+
* (optional) default: true
35+
* - Specified if permissions (ios) and token (android and ios) will requested or not,
36+
* - if not, you must call PushNotificationsHandler.requestPermissions() later
37+
*/
38+
requestPermissions: true,
39+
});
40+
}
41+
42+
localNotif() {
43+
PushNotification.localNotification({
44+
/* Android Only Properties */
45+
id: ''+this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
46+
ticker: "My Notification Ticker", // (optional)
47+
autoCancel: true, // (optional) default: true
48+
largeIcon: "ic_launcher", // (optional) default: "ic_launcher"
49+
smallIcon: "ic_notification", // (optional) default: "ic_notification" with fallback for "ic_launcher"
50+
bigText: "My big text that will be shown when notification is expanded", // (optional) default: "message" prop
51+
subText: "This is a subText", // (optional) default: none
52+
color: "red", // (optional) default: system default
53+
vibrate: true, // (optional) default: true
54+
vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
55+
tag: 'some_tag', // (optional) add tag to message
56+
group: "group", // (optional) add group to message
57+
ongoing: false, // (optional) set whether this is an "ongoing" notification
58+
59+
/* iOS only properties */
60+
alertAction: 'view', // (optional) default: view
61+
category: null, // (optional) default: null
62+
userInfo: null, // (optional) default: null (object containing additional notification data)
63+
64+
/* iOS and Android properties */
65+
title: "My Notification Title", // (optional)
66+
message: "My Notification Message", // (required)
67+
playSound: false, // (optional) default: true
68+
soundName: 'default', // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
69+
number: '10', // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)
70+
actions: '["Yes", "No"]', // (Android only) See the doc for notification actions to know more
71+
});
72+
73+
this.lastId++;
74+
}
75+
76+
scheduleNotif() {
77+
PushNotification.localNotificationSchedule({
78+
date: new Date(Date.now() + (30 * 1000)), // in 30 secs
79+
80+
/* Android Only Properties */
81+
id: ''+this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
82+
ticker: "My Notification Ticker", // (optional)
83+
autoCancel: true, // (optional) default: true
84+
largeIcon: "ic_launcher", // (optional) default: "ic_launcher"
85+
smallIcon: "ic_notification", // (optional) default: "ic_notification" with fallback for "ic_launcher"
86+
bigText: "My big text that will be shown when notification is expanded", // (optional) default: "message" prop
87+
subText: "This is a subText", // (optional) default: none
88+
color: "red", // (optional) default: system default
89+
vibrate: true, // (optional) default: true
90+
vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
91+
tag: 'some_tag', // (optional) add tag to message
92+
group: "group", // (optional) add group to message
93+
ongoing: false, // (optional) set whether this is an "ongoing" notification
94+
95+
/* iOS only properties */
96+
alertAction: 'view', // (optional) default: view
97+
category: null, // (optional) default: null
98+
userInfo: null, // (optional) default: null (object containing additional notification data)
99+
100+
/* iOS and Android properties */
101+
title: "My Notification Title", // (optional)
102+
message: "My Notification Message", // (required)
103+
playSound: false, // (optional) default: true
104+
soundName: 'default', // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
105+
number: '10', // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)
106+
actions: '["Yes", "No"]', // (Android only) See the doc for notification actions to know more
107+
});
108+
109+
this.lastId++;
110+
}
111+
112+
checkPermission(cbk) {
113+
return PushNotification.checkPermissions(cbk);
114+
}
115+
116+
cancelNotif() {
117+
PushNotification.cancelLocalNotifications({id: ''+this.lastId});
118+
}
119+
120+
cancelAll() {
121+
PushNotification.cancelAllLocalNotifications();
122+
}
123+
}

0 commit comments

Comments
 (0)