Skip to content

Commit 0f45858

Browse files
authored
chore(example): update example to function component (#86)
1 parent 8bfab2f commit 0f45858

File tree

2 files changed

+118
-85
lines changed

2 files changed

+118
-85
lines changed

example/App.js

Lines changed: 78 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @flow
66
*/
77

8-
import React, {Component} from 'react';
8+
import React, {useState, useEffect} from 'react';
99
import {
1010
Alert,
1111
StyleSheet,
@@ -29,82 +29,39 @@ class Button extends React.Component<$FlowFixMeProps> {
2929
}
3030
}
3131

32-
type Props = {};
33-
type State = {
34-
permissions: Object,
35-
};
36-
export class App extends Component<Props, State> {
37-
state = {
38-
permissions: {},
39-
};
32+
export const App = () => {
33+
const [permissions, setPermissions] = useState({});
4034

41-
UNSAFE_componentWillMount() {
42-
PushNotificationIOS.addEventListener('register', this._onRegistered);
35+
useEffect(() => {
36+
PushNotificationIOS.requestPermissions();
37+
PushNotificationIOS.addEventListener('register', onRegistered);
4338
PushNotificationIOS.addEventListener(
4439
'registrationError',
45-
this._onRegistrationError,
40+
onRegistrationError,
4641
);
42+
PushNotificationIOS.addEventListener('notification', onRemoteNotification);
4743
PushNotificationIOS.addEventListener(
48-
'notification',
49-
this._onRemoteNotification,
50-
);
51-
PushNotificationIOS.addEventListener(
52-
'localNotification',
53-
this._onLocalNotification,
54-
);
55-
56-
PushNotificationIOS.requestPermissions();
57-
}
58-
59-
componentWillUnmount() {
60-
PushNotificationIOS.removeEventListener('register', this._onRegistered);
61-
PushNotificationIOS.removeEventListener(
62-
'registrationError',
63-
this._onRegistrationError,
64-
);
65-
PushNotificationIOS.removeEventListener(
66-
'notification',
67-
this._onRemoteNotification,
68-
);
69-
PushNotificationIOS.removeEventListener(
7044
'localNotification',
71-
this._onLocalNotification,
45+
onLocalNotification,
7246
);
73-
}
74-
75-
render() {
76-
console.log(PushNotificationIOS);
77-
return (
78-
<View style={styles.container}>
79-
<Button
80-
onPress={this._sendNotification}
81-
label="Send fake notification"
82-
/>
83-
84-
<Button
85-
onPress={this._sendLocalNotification}
86-
label="Send fake local notification"
87-
/>
88-
<Button
89-
onPress={() => PushNotificationIOS.setApplicationIconBadgeNumber(42)}
90-
label="Set app's icon badge to 42"
91-
/>
92-
<Button
93-
onPress={() => PushNotificationIOS.setApplicationIconBadgeNumber(0)}
94-
label="Clear app's icon badge"
95-
/>
96-
<View>
97-
<Button
98-
onPress={this._showPermissions.bind(this)}
99-
label="Show enabled permissions"
100-
/>
101-
<Text>{JSON.stringify(this.state.permissions)}</Text>
102-
</View>
103-
</View>
104-
);
105-
}
106-
107-
_sendNotification() {
47+
return () => {
48+
PushNotificationIOS.removeEventListener('register', onRegistered);
49+
PushNotificationIOS.removeEventListener(
50+
'registrationError',
51+
onRegistrationError,
52+
);
53+
PushNotificationIOS.removeEventListener(
54+
'notification',
55+
onRemoteNotification,
56+
);
57+
PushNotificationIOS.removeEventListener(
58+
'localNotification',
59+
onLocalNotification,
60+
);
61+
};
62+
}, []);
63+
64+
const sendNotification = () => {
10865
DeviceEventEmitter.emit('remoteNotificationReceived', {
10966
remote: true,
11067
aps: {
@@ -115,25 +72,33 @@ export class App extends Component<Props, State> {
11572
'content-available': 1,
11673
},
11774
});
118-
}
75+
};
11976

120-
_sendLocalNotification() {
77+
const sendLocalNotification = () => {
12178
PushNotificationIOS.presentLocalNotification({
12279
alertBody: 'Sample local notification',
80+
fireDate: new Date().toISOString(),
12381
applicationIconBadgeNumber: 1,
12482
});
125-
}
83+
};
12684

127-
_onRegistered(deviceToken) {
85+
const scheduleLocalNotification = () => {
86+
PushNotificationIOS.scheduleLocalNotification({
87+
alertBody: 'Test Local Notification',
88+
fireDate: new Date().toISOString(),
89+
});
90+
};
91+
92+
const onRegistered = deviceToken => {
12893
Alert.alert('Registered For Remote Push', `Device Token: ${deviceToken}`, [
12994
{
13095
text: 'Dismiss',
13196
onPress: null,
13297
},
13398
]);
134-
}
99+
};
135100

136-
_onRegistrationError(error) {
101+
const onRegistrationError = error => {
137102
Alert.alert(
138103
'Failed To Register For Remote Push',
139104
`Error (${error.code}): ${error.message}`,
@@ -144,9 +109,9 @@ export class App extends Component<Props, State> {
144109
},
145110
],
146111
);
147-
}
112+
};
148113

149-
_onRemoteNotification(notification) {
114+
const onRemoteNotification = notification => {
150115
const result = `Message: ${notification.getMessage()};\n
151116
badge: ${notification.getBadgeCount()};\n
152117
sound: ${notification.getSound()};\n
@@ -159,9 +124,9 @@ export class App extends Component<Props, State> {
159124
onPress: null,
160125
},
161126
]);
162-
}
127+
};
163128

164-
_onLocalNotification(notification) {
129+
const onLocalNotification = notification => {
165130
Alert.alert(
166131
'Local Notification Received',
167132
'Alert message: ' + notification.getMessage(),
@@ -172,14 +137,42 @@ export class App extends Component<Props, State> {
172137
},
173138
],
174139
);
175-
}
140+
};
176141

177-
_showPermissions() {
142+
const showPermissions = () => {
178143
PushNotificationIOS.checkPermissions(permissions => {
179-
this.setState({permissions});
144+
setPermissions({permissions});
180145
});
181-
}
182-
}
146+
};
147+
148+
return (
149+
<View style={styles.container}>
150+
<Button onPress={sendNotification} label="Send fake notification" />
151+
152+
<Button
153+
onPress={sendLocalNotification}
154+
label="Send fake local notification"
155+
/>
156+
<Button
157+
onPress={scheduleLocalNotification}
158+
label="Schedule fake local notification"
159+
/>
160+
161+
<Button
162+
onPress={() => PushNotificationIOS.setApplicationIconBadgeNumber(42)}
163+
label="Set app's icon badge to 42"
164+
/>
165+
<Button
166+
onPress={() => PushNotificationIOS.setApplicationIconBadgeNumber(0)}
167+
label="Clear app's icon badge"
168+
/>
169+
<View>
170+
<Button onPress={showPermissions} label="Show enabled permissions" />
171+
<Text>{JSON.stringify(permissions)}</Text>
172+
</View>
173+
</View>
174+
);
175+
};
183176

184177
const styles = StyleSheet.create({
185178
container: {

example/ios/example/AppDelegate.m

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#import <React/RCTBridge.h>
1111
#import <React/RCTBundleURLProvider.h>
1212
#import <React/RCTRootView.h>
13+
#import <RNCPushNotificationIOS.h>
14+
#import <UserNotifications/UserNotifications.h>
1315

1416
@implementation AppDelegate
1517

@@ -27,9 +29,47 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
2729
rootViewController.view = rootView;
2830
self.window.rootViewController = rootViewController;
2931
[self.window makeKeyAndVisible];
32+
33+
// Define UNUserNotificationCenter
34+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
35+
center.delegate = self;
36+
3037
return YES;
3138
}
3239

40+
//Called when a notification is delivered to a foreground app.
41+
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
42+
{
43+
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
44+
}
45+
46+
// Required to register for notifications
47+
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
48+
{
49+
[RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];
50+
}
51+
// Required for the register event.
52+
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
53+
{
54+
[RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
55+
}
56+
// Required for the notification event. You must call the completion handler after handling the remote notification.
57+
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
58+
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
59+
{
60+
[RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
61+
}
62+
// Required for the registrationError event.
63+
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
64+
{
65+
[RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
66+
}
67+
// Required for the localNotification event.
68+
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
69+
{
70+
[RNCPushNotificationIOS didReceiveLocalNotification:notification];
71+
}
72+
3373
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
3474
{
3575
#if DEBUG

0 commit comments

Comments
 (0)