Commit 720709e4 authored by Libin Lu's avatar Libin Lu

update example

parent defb0962
import FirebaseConstants from "./FirebaseConstants"; import FirebaseConstants from "./FirebaseConstants";
import { Platform } from "react-native"; import { Platform, Alert } from "react-native";
const API_URL = "https://fcm.googleapis.com/fcm/send"; const API_URL = "https://fcm.googleapis.com/fcm/send";
...@@ -17,8 +17,7 @@ class FirebaseClient { ...@@ -17,8 +17,7 @@ class FirebaseClient {
"notification":{ "notification":{
"title": "Simple FCM Client", "title": "Simple FCM Client",
"body": "This is a notification with only NOTIFICATION.", "body": "This is a notification with only NOTIFICATION.",
"sound": "default", "sound": "default"
"click_action": "fcm.ACTION.HELLO"
}, },
"priority": 10 "priority": 10
} }
...@@ -47,9 +46,7 @@ class FirebaseClient { ...@@ -47,9 +46,7 @@ class FirebaseClient {
"data":{ "data":{
"title": "Simple FCM Client", "title": "Simple FCM Client",
"body": "This is a notification with only DATA.", "body": "This is a notification with only DATA.",
"sound": "default", "sound": "default"
"click_action": "fcm.ACTION.HELLO",
"remote": true
}, },
"priority": "normal" "priority": "normal"
} }
...@@ -63,14 +60,10 @@ class FirebaseClient { ...@@ -63,14 +60,10 @@ class FirebaseClient {
"notification":{ "notification":{
"title": "Simple FCM Client", "title": "Simple FCM Client",
"body": "This is a notification with NOTIFICATION and DATA (NOTIF).", "body": "This is a notification with NOTIFICATION and DATA (NOTIF).",
"sound": "default", "sound": "default"
"click_action": "fcm.ACTION.HELLO"
}, },
"data":{ "data":{
"title": "Simple FCM Client", "hello": "there"
"body": "This is a notification with NOTIFICATION and DATA (DATA)",
"click_action": "fcm.ACTION.HELLO",
"remote": true
}, },
"priority": "high" "priority": "high"
} }
...@@ -78,16 +71,25 @@ class FirebaseClient { ...@@ -78,16 +71,25 @@ class FirebaseClient {
this._send(JSON.stringify(body), "notification-data"); this._send(JSON.stringify(body), "notification-data");
} }
_send(body, type) { async _send(body, type) {
if(FirebaseClient.KEY === 'YOUR_API_KEY'){
Alert.alert('Set your API_KEY in app/FirebaseConstants.js')
return;
}
let headers = new Headers({ let headers = new Headers({
"Content-Type": "application/json", "Content-Type": "application/json",
"Content-Length": parseInt(body.length),
"Authorization": "key=" + FirebaseConstants.KEY "Authorization": "key=" + FirebaseConstants.KEY
}); });
fetch(API_URL, { method: "POST", headers, body }) try {
.then(response => console.log("Send " + type + " response", response)) let response = await fetch(API_URL, { method: "POST", headers, body });
.catch(error => console.log("Error sending " + type, error)); response = await response.json();
if(!response.success){
Alert.alert('Failed to send notification, check error log')
}
} catch (err) {
Alert.alert(err && err.message)
}
} }
} }
......
...@@ -422,6 +422,14 @@ ...@@ -422,6 +422,14 @@
name = Products; name = Products;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
3AF0A6071F7BE4DC004B899F /* Recovered References */ = {
isa = PBXGroup;
children = (
692E216422234A4CB6A7A838 /* libRNFIRMessaging.a */,
);
name = "Recovered References";
sourceTree = "<group>";
};
4339BFC21DAEBA3900F53B62 /* Products */ = { 4339BFC21DAEBA3900F53B62 /* Products */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
...@@ -497,6 +505,7 @@ ...@@ -497,6 +505,7 @@
00E356EF1AD99517003FC87E /* SimpleFcmClientTests */, 00E356EF1AD99517003FC87E /* SimpleFcmClientTests */,
83CBBA001A601CBA00E9B192 /* Products */, 83CBBA001A601CBA00E9B192 /* Products */,
7E63D0154B6803F90ADF7C78 /* Pods */, 7E63D0154B6803F90ADF7C78 /* Pods */,
3AF0A6071F7BE4DC004B899F /* Recovered References */,
); );
indentWidth = 2; indentWidth = 2;
sourceTree = "<group>"; sourceTree = "<group>";
......
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
[RNFIRMessaging willPresentNotification:notification withCompletionHandler:completionHandler]; [RNFIRMessaging willPresentNotification:notification withCompletionHandler:completionHandler];
} }
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{ {
[RNFIRMessaging didReceiveNotificationResponse:response withCompletionHandler:completionHandler]; [RNFIRMessaging didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
} }
......
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