Skip to content

Commit

Permalink
update example
Browse files Browse the repository at this point in the history
  • Loading branch information
Libin Lu committed Feb 13, 2018
1 parent 2e4230f commit 41970d9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
18 changes: 9 additions & 9 deletions Examples/simple-fcm-client/app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,6 @@ export default class App extends Component {
Welcome to Simple Fcm Client!
</Text>

<Text>
Init notif: {JSON.stringify(this.state.initNotif)}

</Text>

<Text selectable={true} onPress={() => this.setClipboardContent(this.state.token)} style={styles.instructions}>
Token: {this.state.token}
</Text>

<Text style={styles.feedback}>
{this.state.tokenCopyFeedback}
</Text>
Expand Down Expand Up @@ -189,6 +180,15 @@ export default class App extends Component {
<TouchableOpacity onPress={() => this.scheduleLocalNotification()} style={styles.button}>
<Text style={styles.buttonText}>Schedule Notification in 5s</Text>
</TouchableOpacity>

<Text>
Init notif: {JSON.stringify(this.state.initNotif)}
</Text>

<Text selectable={true} onPress={() => this.setClipboardContent(this.state.token)} style={styles.instructions}>
Token: {this.state.token}
</Text>

</View>
);
}
Expand Down
51 changes: 42 additions & 9 deletions Examples/simple-fcm-client/app/Listeners.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Platform, AsyncStorage } from 'react-native';
import { Platform, AsyncStorage, AppState } from 'react-native';

import FCM, {FCMEvent, RemoteNotificationResult, WillPresentNotificationResult, NotificationType, NotificationActionType, NotificationActionOption, NotificationCategoryOption} from "react-native-fcm";

Expand All @@ -10,10 +10,33 @@ AsyncStorage.getItem('lastNotification').then(data=>{
}
})

AsyncStorage.getItem('lastMessage').then(data=>{
if(data){
// if notification arrives when app is killed, it should still be logged here
console.log('last message', JSON.parse(data));
AsyncStorage.removeItem('lastMessage');
}
})

export function registerKilledListener(){
// these callback will be triggered even when app is killed
FCM.on(FCMEvent.Notification, notif => {
AsyncStorage.setItem('lastNotification', JSON.stringify(notif));
if(notif.opened_from_tray){
if(notif._actionIdentifier === 'com.myidentifi.fcm.text.reply'){
if(AppState.currentState !== 'background'){
alert('User replied '+ JSON.stringify(notif._userText));
} else {
AsyncStorage.setItem('lastMessage', JSON.stringify(notif._userText));
}
}
if(notif._actionIdentifier === 'com.myidentifi.fcm.text.view'){
alert("User clicked View in App");
}
if(notif._actionIdentifier === 'com.myidentifi.fcm.text.dismiss'){
alert("User clicked Dismiss");
}
}
});
}

Expand All @@ -22,13 +45,15 @@ export function registerAppListener(){
FCM.on(FCMEvent.Notification, notif => {
console.log("Notification", notif);

if(Platform.OS ==='ios' && notif._notificationType === NotificationType.WillPresent && !notif.local_notification){
// this notification is only to decide if you want to show the notification when user if in forground.
// usually you can ignore it. just decide to show or not.
notif.finish(WillPresentNotificationResult.All)
return;
}

if(notif.opened_from_tray){
if(notif._actionIdentifier === 'com.myidentifi.fcm.text.reply'){
alert("User replied: "+notif._userText);
}
if(notif._actionIdentifier === 'com.myidentifi.fcm.text.dismiss'){
alert("User clicked Dismiss");
}

}

if(Platform.OS ==='ios'){
Expand Down Expand Up @@ -72,17 +97,25 @@ FCM.setNotificationCategories([
{
type: NotificationActionType.TextInput,
id: 'com.myidentifi.fcm.text.reply',
title: 'Reply',
title: 'Quick Reply',
textInputButtonTitle: 'Send',
textInputPlaceholder: 'Say something',
intentIdentifiers: [],
options: NotificationActionOption.AuthenticationRequired
},
{
type: NotificationActionType.Default,
id: 'com.myidentifi.fcm.text.view',
title: 'View in App',
intentIdentifiers: [],
options: NotificationActionOption.Foreground
},
{
type: NotificationActionType.Default,
id: 'com.myidentifi.fcm.text.dismiss',
title: 'Dismiss',
intentIdentifiers: []
intentIdentifiers: [],
options: NotificationActionOption.Destructive
}
],
options: [NotificationCategoryOption.CustomDismissAction, NotificationCategoryOption.PreviewsShowTitle]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -83,6 +84,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down

0 comments on commit 41970d9

Please sign in to comment.