Skip to content

Commit 4a9eea8

Browse files
committed
added messageId to messaging service broadcast/event and cleaned up
1 parent d585fca commit 4a9eea8

File tree

2 files changed

+178
-163
lines changed

2 files changed

+178
-163
lines changed

android/src/main/java/io/fullstack/firestack/FirestackMessagingService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
1818
Log.d(TAG, "Remote message received");
1919
// debug
2020
Log.d(TAG, "From: " + remoteMessage.getFrom());
21+
2122
if (remoteMessage.getData().size() > 0) {
2223
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
2324
}
25+
2426
if (remoteMessage.getNotification() != null) {
2527
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
2628
}
27-
if (remoteMessage.getNotification() != null) {
2829

29-
}
3030
Intent i = new Intent(FirestackMessaging.INTENT_NAME_NOTIFICATION);
3131
i.putExtra("data", remoteMessage);
3232
sendOrderedBroadcast(i, null);

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

Lines changed: 176 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -27,177 +27,192 @@
2727

2828
public class FirestackMessaging extends ReactContextBaseJavaModule {
2929

30-
private static final String TAG = "FirestackMessaging";
31-
private static final String EVENT_NAME_TOKEN = "FirestackRefreshToken";
32-
private static final String EVENT_NAME_NOTIFICATION = "FirestackReceiveNotification";
33-
private static final String EVENT_NAME_SEND = "FirestackUpstreamSend";
34-
35-
public static final String INTENT_NAME_TOKEN = "io.fullstack.firestack.refreshToken";
36-
public static final String INTENT_NAME_NOTIFICATION = "io.fullstack.firestack.ReceiveNotification";
37-
public static final String INTENT_NAME_SEND = "io.fullstack.firestack.Upstream";
38-
39-
private IntentFilter mRefreshTokenIntentFilter;
40-
private IntentFilter mReceiveNotificationIntentFilter;
41-
private IntentFilter mReceiveSendIntentFilter;
42-
43-
public FirestackMessaging(ReactApplicationContext reactContext) {
44-
super(reactContext);
45-
mRefreshTokenIntentFilter = new IntentFilter(INTENT_NAME_TOKEN);
46-
mReceiveNotificationIntentFilter = new IntentFilter(INTENT_NAME_NOTIFICATION);
47-
mReceiveSendIntentFilter = new IntentFilter(INTENT_NAME_SEND);
48-
initRefreshTokenHandler();
49-
initMessageHandler();
50-
initSendHandler();
51-
Log.d(TAG, "New instance");
52-
}
53-
54-
@Override
55-
public String getName() {
56-
return TAG;
57-
}
58-
59-
@ReactMethod
60-
public void getToken(final Callback callback) {
61-
62-
try {
63-
String token = FirebaseInstanceId.getInstance().getToken();
64-
Log.d(TAG, "Firebase token: " + token);
65-
callback.invoke(null, token);
66-
} catch (Exception e) {
67-
WritableMap error = Arguments.createMap();
68-
error.putString("message", e.getMessage());
69-
callback.invoke(error);
70-
}
71-
}
72-
73-
/**
74-
*
75-
*/
76-
private void initRefreshTokenHandler() {
77-
getReactApplicationContext().registerReceiver(new BroadcastReceiver() {
78-
@Override
79-
public void onReceive(Context context, Intent intent) {
80-
WritableMap params = Arguments.createMap();
81-
params.putString("token", intent.getStringExtra("token"));
82-
ReactContext ctx = getReactApplicationContext();
83-
Log.d(TAG, "initRefreshTokenHandler received event " + EVENT_NAME_TOKEN);
84-
Utils.sendEvent(ctx, EVENT_NAME_TOKEN, params);
30+
private static final String TAG = "FirestackMessaging";
31+
private static final String EVENT_NAME_TOKEN = "FirestackRefreshToken";
32+
private static final String EVENT_NAME_NOTIFICATION = "FirestackReceiveNotification";
33+
private static final String EVENT_NAME_SEND = "FirestackUpstreamSend";
34+
35+
public static final String INTENT_NAME_TOKEN = "io.fullstack.firestack.refreshToken";
36+
public static final String INTENT_NAME_NOTIFICATION = "io.fullstack.firestack.ReceiveNotification";
37+
public static final String INTENT_NAME_SEND = "io.fullstack.firestack.Upstream";
38+
39+
private IntentFilter mRefreshTokenIntentFilter;
40+
private IntentFilter mReceiveNotificationIntentFilter;
41+
private IntentFilter mReceiveSendIntentFilter;
42+
private BroadcastReceiver mBroadcastReceiver;
43+
44+
public FirestackMessaging(ReactApplicationContext reactContext) {
45+
super(reactContext);
46+
mRefreshTokenIntentFilter = new IntentFilter(INTENT_NAME_TOKEN);
47+
mReceiveNotificationIntentFilter = new IntentFilter(INTENT_NAME_NOTIFICATION);
48+
mReceiveSendIntentFilter = new IntentFilter(INTENT_NAME_SEND);
49+
initRefreshTokenHandler();
50+
initMessageHandler();
51+
initSendHandler();
52+
Log.d(TAG, "New instance");
53+
}
54+
55+
@Override
56+
public String getName() {
57+
return TAG;
58+
}
59+
60+
private void initMessageHandler() {
61+
Log.d(TAG, "Firestack initMessageHandler called");
62+
63+
if (mBroadcastReceiver == null) {
64+
mBroadcastReceiver = new BroadcastReceiver() {
65+
@Override
66+
public void onReceive(Context context, Intent intent) {
67+
RemoteMessage remoteMessage = intent.getParcelableExtra("data");
68+
Log.d(TAG, "Firebase onReceive: " + remoteMessage);
69+
WritableMap params = Arguments.createMap();
70+
71+
params.putNull("data");
72+
params.putNull("notification");
73+
params.putString("id", remoteMessage.getMessageId());
74+
params.putString("messageId", remoteMessage.getMessageId());
75+
76+
77+
if (remoteMessage.getData().size() != 0) {
78+
WritableMap dataMap = Arguments.createMap();
79+
Map<String, String> data = remoteMessage.getData();
80+
81+
for (String key : data.keySet()) {
82+
dataMap.putString(key, data.get(key));
8583
}
8684

87-
;
88-
}, mRefreshTokenIntentFilter);
89-
}
85+
params.putMap("data", dataMap);
86+
}
9087

91-
@ReactMethod
92-
public void subscribeToTopic(String topic, final Callback callback) {
93-
try {
94-
FirebaseMessaging.getInstance().subscribeToTopic(topic);
95-
callback.invoke(null,topic);
96-
} catch (Exception e) {
97-
e.printStackTrace();
98-
Log.d(TAG, "Firebase token: " + e);
99-
WritableMap error = Arguments.createMap();
100-
error.putString("message", e.getMessage());
101-
callback.invoke(error);
10288

103-
}
104-
}
89+
if (remoteMessage.getNotification() != null) {
90+
WritableMap notificationMap = Arguments.createMap();
91+
RemoteMessage.Notification notification = remoteMessage.getNotification();
92+
notificationMap.putString("title", notification.getTitle());
93+
notificationMap.putString("body", notification.getBody());
94+
notificationMap.putString("icon", notification.getIcon());
95+
notificationMap.putString("sound", notification.getSound());
96+
notificationMap.putString("tag", notification.getTag());
97+
params.putMap("notification", notificationMap);
98+
}
10599

106-
@ReactMethod
107-
public void unsubscribeFromTopic(String topic, final Callback callback) {
108-
try {
109-
FirebaseMessaging.getInstance().unsubscribeFromTopic(topic);
110-
callback.invoke(null,topic);
111-
} catch (Exception e) {
112-
WritableMap error = Arguments.createMap();
113-
error.putString("message", e.getMessage());
114-
callback.invoke(error);
100+
ReactContext ctx = getReactApplicationContext();
101+
Utils.sendEvent(ctx, EVENT_NAME_NOTIFICATION, params);
115102
}
116-
}
103+
};
117104

118-
private void initMessageHandler() {
119-
Log.d(TAG, "Firestack initMessageHandler called");
120-
getReactApplicationContext().registerReceiver(new BroadcastReceiver() {
121-
@Override
122-
public void onReceive(Context context, Intent intent) {
123-
RemoteMessage remoteMessage = intent.getParcelableExtra("data");
124-
Log.d(TAG, "Firebase onReceive: " + remoteMessage);
125-
WritableMap params = Arguments.createMap();
126-
if (remoteMessage.getData().size() != 0) {
127-
WritableMap dataMap = Arguments.createMap();
128-
Map<String, String> data = remoteMessage.getData();
129-
//Set<String> keysIterator = data.keySet();
130-
for (String key : data.keySet()) {
131-
dataMap.putString(key, data.get(key));
132-
}
133-
params.putMap("data", dataMap);
134-
} else {
135-
params.putNull("data");
136-
}
137-
if (remoteMessage.getNotification() != null) {
138-
WritableMap notificationMap = Arguments.createMap();
139-
RemoteMessage.Notification notification = remoteMessage.getNotification();
140-
notificationMap.putString("title", notification.getTitle());
141-
notificationMap.putString("body", notification.getBody());
142-
notificationMap.putString("icon", notification.getIcon());
143-
notificationMap.putString("sound", notification.getSound());
144-
notificationMap.putString("tag", notification.getTag());
145-
params.putMap("notification", notificationMap);
146-
} else {
147-
params.putNull("notification");
148-
}
149-
ReactContext ctx = getReactApplicationContext();
150-
Utils.sendEvent(ctx, EVENT_NAME_NOTIFICATION, params);
151-
}
152-
}, mReceiveNotificationIntentFilter);
153105
}
106+
getReactApplicationContext().registerReceiver(mBroadcastReceiver, mReceiveNotificationIntentFilter);
107+
}
108+
109+
/**
110+
*
111+
*/
112+
private void initRefreshTokenHandler() {
113+
getReactApplicationContext().registerReceiver(new BroadcastReceiver() {
114+
@Override
115+
public void onReceive(Context context, Intent intent) {
116+
WritableMap params = Arguments.createMap();
117+
params.putString("token", intent.getStringExtra("token"));
118+
ReactContext ctx = getReactApplicationContext();
119+
Log.d(TAG, "initRefreshTokenHandler received event " + EVENT_NAME_TOKEN);
120+
Utils.sendEvent(ctx, EVENT_NAME_TOKEN, params);
121+
}
122+
123+
;
124+
}, mRefreshTokenIntentFilter);
125+
}
126+
127+
@ReactMethod
128+
public void subscribeToTopic(String topic, final Callback callback) {
129+
try {
130+
FirebaseMessaging.getInstance().subscribeToTopic(topic);
131+
callback.invoke(null, topic);
132+
} catch (Exception e) {
133+
e.printStackTrace();
134+
Log.d(TAG, "Firebase token: " + e);
135+
WritableMap error = Arguments.createMap();
136+
error.putString("message", e.getMessage());
137+
callback.invoke(error);
154138

155-
@ReactMethod
156-
public void send(String senderId, String messageId, String messageType, ReadableMap params, final Callback callback) {
157-
FirebaseMessaging fm = FirebaseMessaging.getInstance();
158-
RemoteMessage.Builder remoteMessage = new RemoteMessage.Builder(senderId);
159-
remoteMessage.setMessageId(messageId);
160-
remoteMessage.setMessageType(messageType);
161-
ReadableMapKeySetIterator iterator = params.keySetIterator();
162-
while (iterator.hasNextKey()) {
163-
String key = iterator.nextKey();
164-
ReadableType type = params.getType(key);
165-
if (type == ReadableType.String) {
166-
remoteMessage.addData(key, params.getString(key));
167-
Log.d(TAG, "Firebase send: " + key);
168-
Log.d(TAG, "Firebase send: " + params.getString(key));
169-
}
170-
}
171-
try {
172-
fm.send(remoteMessage.build());
173-
WritableMap res = Arguments.createMap();
174-
res.putString("status", "success");
175-
callback.invoke(null, res);
176-
} catch(Exception e) {
177-
Log.e(TAG, "Error sending message", e);
178-
WritableMap error = Arguments.createMap();
179-
error.putString("code", e.toString());
180-
error.putString("message", e.toString());
181-
callback.invoke(error);
182-
}
139+
}
140+
}
141+
142+
@ReactMethod
143+
public void getToken(final Callback callback) {
144+
145+
try {
146+
String token = FirebaseInstanceId.getInstance().getToken();
147+
Log.d(TAG, "Firebase token: " + token);
148+
callback.invoke(null, token);
149+
} catch (Exception e) {
150+
WritableMap error = Arguments.createMap();
151+
error.putString("message", e.getMessage());
152+
callback.invoke(error);
153+
}
154+
}
155+
156+
@ReactMethod
157+
public void unsubscribeFromTopic(String topic, final Callback callback) {
158+
try {
159+
FirebaseMessaging.getInstance().unsubscribeFromTopic(topic);
160+
callback.invoke(null, topic);
161+
} catch (Exception e) {
162+
WritableMap error = Arguments.createMap();
163+
error.putString("message", e.getMessage());
164+
callback.invoke(error);
165+
}
166+
}
167+
168+
@ReactMethod
169+
public void send(String senderId, String messageId, String messageType, ReadableMap params, final Callback callback) {
170+
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();
175+
176+
while (iterator.hasNextKey()) {
177+
String key = iterator.nextKey();
178+
ReadableType type = params.getType(key);
179+
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));
183+
}
183184
}
184185

185-
private void initSendHandler() {
186-
getReactApplicationContext().registerReceiver(new BroadcastReceiver() {
187-
@Override
188-
public void onReceive(Context context, Intent intent) {
189-
WritableMap params = Arguments.createMap();
190-
if (intent.getBooleanExtra("hasError", false)) {
191-
WritableMap error = Arguments.createMap();
192-
error.putInt("code", intent.getIntExtra("errCode", 0));
193-
error.putString("message", intent.getStringExtra("errorMessage"));
194-
params.putMap("err", error);
195-
} else {
196-
params.putNull("err");
197-
}
198-
ReactContext ctx = getReactApplicationContext();
199-
Utils.sendEvent(ctx, EVENT_NAME_SEND, params);
200-
}
201-
}, mReceiveSendIntentFilter);
186+
try {
187+
fm.send(remoteMessage.build());
188+
WritableMap res = Arguments.createMap();
189+
res.putString("status", "success");
190+
callback.invoke(null, res);
191+
} catch (Exception e) {
192+
Log.e(TAG, "Error sending message", e);
193+
WritableMap error = Arguments.createMap();
194+
error.putString("code", e.toString());
195+
error.putString("message", e.toString());
196+
callback.invoke(error);
202197
}
198+
}
199+
200+
private void initSendHandler() {
201+
getReactApplicationContext().registerReceiver(new BroadcastReceiver() {
202+
@Override
203+
public void onReceive(Context context, Intent intent) {
204+
WritableMap params = Arguments.createMap();
205+
if (intent.getBooleanExtra("hasError", false)) {
206+
WritableMap error = Arguments.createMap();
207+
error.putInt("code", intent.getIntExtra("errCode", 0));
208+
error.putString("message", intent.getStringExtra("errorMessage"));
209+
params.putMap("err", error);
210+
} else {
211+
params.putNull("err");
212+
}
213+
ReactContext ctx = getReactApplicationContext();
214+
Utils.sendEvent(ctx, EVENT_NAME_SEND, params);
215+
}
216+
}, mReceiveSendIntentFilter);
217+
}
203218
}

0 commit comments

Comments
 (0)