55 * @flow
66 */
77
8- import React , { Component } from 'react' ;
8+ import React , { useState , useEffect } from 'react' ;
99import {
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
184177const styles = StyleSheet . create ( {
185178 container : {
0 commit comments