Skip to content

chore(example): update example to function component #86

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 78 additions & 85 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @flow
*/

import React, {Component} from 'react';
import React, {useState, useEffect} from 'react';
import {
Alert,
StyleSheet,
Expand All @@ -29,82 +29,39 @@ class Button extends React.Component<$FlowFixMeProps> {
}
}

type Props = {};
type State = {
permissions: Object,
};
export class App extends Component<Props, State> {
state = {
permissions: {},
};
export const App = () => {
const [permissions, setPermissions] = useState({});

UNSAFE_componentWillMount() {
PushNotificationIOS.addEventListener('register', this._onRegistered);
useEffect(() => {
PushNotificationIOS.requestPermissions();
PushNotificationIOS.addEventListener('register', onRegistered);
PushNotificationIOS.addEventListener(
'registrationError',
this._onRegistrationError,
onRegistrationError,
);
PushNotificationIOS.addEventListener('notification', onRemoteNotification);
PushNotificationIOS.addEventListener(
'notification',
this._onRemoteNotification,
);
PushNotificationIOS.addEventListener(
'localNotification',
this._onLocalNotification,
);

PushNotificationIOS.requestPermissions();
}

componentWillUnmount() {
PushNotificationIOS.removeEventListener('register', this._onRegistered);
PushNotificationIOS.removeEventListener(
'registrationError',
this._onRegistrationError,
);
PushNotificationIOS.removeEventListener(
'notification',
this._onRemoteNotification,
);
PushNotificationIOS.removeEventListener(
'localNotification',
this._onLocalNotification,
onLocalNotification,
);
}

render() {
console.log(PushNotificationIOS);
return (
<View style={styles.container}>
<Button
onPress={this._sendNotification}
label="Send fake notification"
/>

<Button
onPress={this._sendLocalNotification}
label="Send fake local notification"
/>
<Button
onPress={() => PushNotificationIOS.setApplicationIconBadgeNumber(42)}
label="Set app's icon badge to 42"
/>
<Button
onPress={() => PushNotificationIOS.setApplicationIconBadgeNumber(0)}
label="Clear app's icon badge"
/>
<View>
<Button
onPress={this._showPermissions.bind(this)}
label="Show enabled permissions"
/>
<Text>{JSON.stringify(this.state.permissions)}</Text>
</View>
</View>
);
}

_sendNotification() {
return () => {
PushNotificationIOS.removeEventListener('register', onRegistered);
PushNotificationIOS.removeEventListener(
'registrationError',
onRegistrationError,
);
PushNotificationIOS.removeEventListener(
'notification',
onRemoteNotification,
);
PushNotificationIOS.removeEventListener(
'localNotification',
onLocalNotification,
);
};
}, []);

const sendNotification = () => {
DeviceEventEmitter.emit('remoteNotificationReceived', {
remote: true,
aps: {
Expand All @@ -115,25 +72,33 @@ export class App extends Component<Props, State> {
'content-available': 1,
},
});
}
};

_sendLocalNotification() {
const sendLocalNotification = () => {
PushNotificationIOS.presentLocalNotification({
alertBody: 'Sample local notification',
fireDate: new Date().toISOString(),
applicationIconBadgeNumber: 1,
});
}
};

_onRegistered(deviceToken) {
const scheduleLocalNotification = () => {
PushNotificationIOS.scheduleLocalNotification({
alertBody: 'Test Local Notification',
fireDate: new Date().toISOString(),
});
};

const onRegistered = deviceToken => {
Alert.alert('Registered For Remote Push', `Device Token: ${deviceToken}`, [
{
text: 'Dismiss',
onPress: null,
},
]);
}
};

_onRegistrationError(error) {
const onRegistrationError = error => {
Alert.alert(
'Failed To Register For Remote Push',
`Error (${error.code}): ${error.message}`,
Expand All @@ -144,9 +109,9 @@ export class App extends Component<Props, State> {
},
],
);
}
};

_onRemoteNotification(notification) {
const onRemoteNotification = notification => {
const result = `Message: ${notification.getMessage()};\n
badge: ${notification.getBadgeCount()};\n
sound: ${notification.getSound()};\n
Expand All @@ -159,9 +124,9 @@ export class App extends Component<Props, State> {
onPress: null,
},
]);
}
};

_onLocalNotification(notification) {
const onLocalNotification = notification => {
Alert.alert(
'Local Notification Received',
'Alert message: ' + notification.getMessage(),
Expand All @@ -172,14 +137,42 @@ export class App extends Component<Props, State> {
},
],
);
}
};

_showPermissions() {
const showPermissions = () => {
PushNotificationIOS.checkPermissions(permissions => {
this.setState({permissions});
setPermissions({permissions});
});
}
}
};

return (
<View style={styles.container}>
<Button onPress={sendNotification} label="Send fake notification" />

<Button
onPress={sendLocalNotification}
label="Send fake local notification"
/>
<Button
onPress={scheduleLocalNotification}
label="Schedule fake local notification"
/>

<Button
onPress={() => PushNotificationIOS.setApplicationIconBadgeNumber(42)}
label="Set app's icon badge to 42"
/>
<Button
onPress={() => PushNotificationIOS.setApplicationIconBadgeNumber(0)}
label="Clear app's icon badge"
/>
<View>
<Button onPress={showPermissions} label="Show enabled permissions" />
<Text>{JSON.stringify(permissions)}</Text>
</View>
</View>
);
};

const styles = StyleSheet.create({
container: {
Expand Down
40 changes: 40 additions & 0 deletions example/ios/example/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <RNCPushNotificationIOS.h>
#import <UserNotifications/UserNotifications.h>

@implementation AppDelegate

Expand All @@ -27,9 +29,47 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];

// Define UNUserNotificationCenter
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;

return YES;
}

//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}

// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];
}
// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
// Required for the notification event. You must call the completion handler after handling the remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
[RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
[RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
}
// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[RNCPushNotificationIOS didReceiveLocalNotification:notification];
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
Expand Down