Commit e1b1b418 authored by Libin Lu's avatar Libin Lu

update example for v6

parent 3ca8da06
import React, { Component } from "react";
import FCM from "react-native-fcm";
import FCM, {FCMEvent} from "react-native-fcm";
import firebaseClient from "./FirebaseClient";
......@@ -21,21 +21,42 @@ export default class PushController extends Component {
console.log("INITIAL NOTIFICATION", notif)
});
this.notificationUnsubscribe = FCM.on("notification", notif => {
this.notificationListner = FCM.on(FCMEvent.Notification, notif => {
console.log("Notification", notif);
if (notif && notif.local) {
if(notif.local_notification){
return;
}
this.sendRemote(notif);
if(notif.opened_from_tray){
return;
}
if(Platform.OS ==='ios'){
//optional
//iOS requires developers to call completionHandler to end notification process. If you do not call it your background remote notifications could be throttled, to read more about it see the above documentation link.
//This library handles it for you automatically with default behavior (for remote notification, finish with NoData; for WillPresent, finish depend on "show_in_foreground"). However if you want to return different result, follow the following code to override
//notif._notificationType is available for iOS platfrom
switch(notif._notificationType){
case NotificationType.Remote:
notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed
break;
case NotificationType.NotificationResponse:
notif.finish();
break;
case NotificationType.WillPresent:
notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None
break;
}
}
this.showLocalNotification(notif);
});
this.refreshUnsubscribe = FCM.on("refreshToken", token => {
this.refreshTokenListener = FCM.on(FCMEvent.RefreshToken, token => {
console.log("TOKEN (refreshUnsubscribe)", token);
this.props.onChangeToken(token);
});
}
sendRemote(notif) {
showLocalNotification(notif) {
FCM.presentLocalNotification({
title: notif.title,
body: notif.body,
......@@ -47,8 +68,8 @@ export default class PushController extends Component {
}
componentWillUnmount() {
this.refreshUnsubscribe();
this.notificationUnsubscribe();
this.notificationListner.remove();
this.refreshTokenListener.remove();
}
......
......@@ -42,28 +42,20 @@
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
[[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:notification.request.content.userInfo];
if([[notification.request.content.userInfo valueForKey:@"show_in_foreground"] isEqual:@YES]){
completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound);
}else{
completionHandler(UNNotificationPresentationOptionNone);
}
[RNFIRMessaging willPresentNotification:notification withCompletionHandler:completionHandler];
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
NSDictionary* userInfo = [[NSMutableDictionary alloc] initWithDictionary: response.notification.request.content.userInfo];
[userInfo setValue:@YES forKey:@"opened_from_tray"];
[[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:userInfo];
[RNFIRMessaging didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
}
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:notification.userInfo];
[RNFIRMessaging didReceiveLocalNotification:notification];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
[[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:userInfo];
completionHandler(UIBackgroundFetchResultNoData);
[RNFIRMessaging didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
@end
......@@ -9,7 +9,7 @@
"dependencies": {
"react": "~15.4.0-rc.4",
"react-native": "^0.40.0",
"react-native-fcm": "^4.0.0"
"react-native-fcm": "^6.0.2"
},
"jest": {
"preset": "jest-react-native"
......
......@@ -2543,13 +2543,13 @@ mime-db@~1.23.0:
version "1.23.0"
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.23.0.tgz#a31b4070adaea27d732ea333740a64d0ec9a6659"
mime-types@2.1.11, mime-types@~2.1.7:
mime-types@2.1.11, mime-types@~2.1.7, mime-types@~2.1.9:
version "2.1.11"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.11.tgz#c259c471bda808a85d6cd193b430a5fae4473b3c"
dependencies:
mime-db "~1.23.0"
mime-types@^2.1.12, mime-types@~2.1.11, mime-types@~2.1.13, mime-types@~2.1.6, mime-types@~2.1.9:
mime-types@^2.1.12, mime-types@~2.1.11, mime-types@~2.1.13, mime-types@~2.1.6:
version "2.1.13"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.13.tgz#e07aaa9c6c6b9a7ca3012c69003ad25a39e92a88"
dependencies:
......@@ -2955,9 +2955,9 @@ react-deep-force-update@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/react-deep-force-update/-/react-deep-force-update-1.0.1.tgz#f911b5be1d2a6fe387507dd6e9a767aa2924b4c7"
react-native-fcm@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/react-native-fcm/-/react-native-fcm-4.0.0.tgz#c87bd442cdf05c900c7d436da518b070ece19162"
react-native-fcm@^6.0.2:
version "6.0.2"
resolved "https://registry.yarnpkg.com/react-native-fcm/-/react-native-fcm-6.0.2.tgz#432578deaf9ad0fa968424c1babb5563c68eaa7e"
react-native@^0.40.0:
version "0.40.0"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment