Skip to content

Commit 2c3eb5b

Browse files
committed
messaging.send() now only accepts an object of type RemoteMessage as defined in messaging.js - this now includes all the previously missing options such as ttl and collapse key
@chrisbianca / note to self - ios needs doing ;p
1 parent 87b724a commit 2c3eb5b

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

android/src/main/java/io/fullstack/firestack/messaging/FirestackMessaging.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,31 +165,42 @@ public void unsubscribeFromTopic(String topic, final Callback callback) {
165165
}
166166
}
167167

168+
// String senderId, String messageId, String messageType,
168169
@ReactMethod
169-
public void send(String senderId, String messageId, String messageType, ReadableMap params, final Callback callback) {
170+
public void send(ReadableMap params, final Callback callback) {
171+
ReadableMap data = params.getMap("data");
170172
FirebaseMessaging fm = FirebaseMessaging.getInstance();
171-
RemoteMessage.Builder remoteMessage = new RemoteMessage.Builder(senderId);
172-
remoteMessage.setMessageId(messageId);
173-
remoteMessage.setMessageType(messageType);
174-
ReadableMapKeySetIterator iterator = params.keySetIterator();
173+
RemoteMessage.Builder remoteMessage = new RemoteMessage.Builder(params.getString("sender"));
174+
175+
remoteMessage.setMessageId(params.getString("id"));
176+
remoteMessage.setMessageType(params.getString("type"));
177+
178+
if (params.hasKey("ttl")) {
179+
remoteMessage.setTtl(params.getInt("ttl"));
180+
}
181+
182+
if (params.hasKey("collapseKey")) {
183+
remoteMessage.setCollapseKey(params.getString("collapseKey"));
184+
}
185+
186+
ReadableMapKeySetIterator iterator = data.keySetIterator();
175187

176188
while (iterator.hasNextKey()) {
177189
String key = iterator.nextKey();
178-
ReadableType type = params.getType(key);
190+
ReadableType type = data.getType(key);
179191
if (type == ReadableType.String) {
180-
remoteMessage.addData(key, params.getString(key));
181-
Log.d(TAG, "Firebase send: " + key);
182-
Log.d(TAG, "Firebase send: " + params.getString(key));
192+
remoteMessage.addData(key, data.getString(key));
183193
}
184194
}
185195

186196
try {
187197
fm.send(remoteMessage.build());
188198
WritableMap res = Arguments.createMap();
189199
res.putString("status", "success");
200+
Log.d(TAG, "send: Message sent");
190201
callback.invoke(null, res);
191202
} catch (Exception e) {
192-
Log.e(TAG, "Error sending message", e);
203+
Log.e(TAG, "send: error sending message", e);
193204
WritableMap error = Arguments.createMap();
194205
error.putString("code", e.toString());
195206
error.putString("message", e.toString());

lib/modules/messaging.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import { promisify } from '../utils';
55
const FirestackMessaging = NativeModules.FirestackMessaging || NativeModules.FirestackCloudMessaging;
66
const FirestackMessagingEvt = new NativeEventEmitter(FirestackMessaging);
77

8+
type RemoteMessage = {
9+
id: string,
10+
type: string,
11+
ttl?: number,
12+
sender: string,
13+
collapseKey?: string,
14+
data: Object,
15+
};
16+
817
/**
918
* @class Messaging
1019
*/
@@ -39,20 +48,21 @@ export default class Messaging extends Base {
3948
return promisify('getToken', FirestackMessaging)();
4049
}
4150

42-
sendMessage(details: Object = {}, type: string = 'local') {
43-
const methodName = `send${type == 'local' ? 'Local' : 'Remote'}`;
44-
this.log.info('sendMessage', methodName, details);
45-
return promisify(methodName, FirestackMessaging)(details);
46-
}
47-
48-
scheduleMessage(details: Object = {}, type: string = 'local') {
49-
const methodName = `schedule${type == 'local' ? 'Local' : 'Remote'}`;
50-
return promisify(methodName, FirestackMessaging)(details);
51-
}
51+
// sendMessage(details: Object = {}, type: string = 'local') {
52+
// const methodName = `send${type == 'local' ? 'Local' : 'Remote'}`;
53+
// this.log.info('sendMessage', methodName, details);
54+
// return promisify(methodName, FirestackMessaging)(details);
55+
// }
56+
//
57+
// scheduleMessage(details: Object = {}, type: string = 'local') {
58+
// const methodName = `schedule${type == 'local' ? 'Local' : 'Remote'}`;
59+
// return promisify(methodName, FirestackMessaging)(details);
60+
// }
5261

5362
// OLD
54-
send(senderId, messageId, messageType, msg) {
55-
return promisify('send', FirestackMessaging)(senderId, messageId, messageType, msg);
63+
send(remoteMessage: RemoteMessage) {
64+
if (!remoteMessage || !remoteMessage.data) return Promise.reject(new Error('Invalid remote message format provided.'));
65+
return promisify('send', FirestackMessaging)(remoteMessage);
5666
}
5767

5868
//

0 commit comments

Comments
 (0)