Skip to content

Commit 2235181

Browse files
committed
Remove Intent-Filter Usage
• Switches the Android implementation to use the notification opened & received handlers directly instead of using intent filters
1 parent b05e7be commit 2235181

File tree

4 files changed

+41
-163
lines changed

4 files changed

+41
-163
lines changed

android/src/main/java/com/geektime/rnonesignalandroid/NotificationOpenedHandler.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

android/src/main/java/com/geektime/rnonesignalandroid/NotificationReceivedHandler.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

android/src/main/java/com/geektime/rnonesignalandroid/RNOneSignal.java

Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import android.app.Activity;
66
import android.content.BroadcastReceiver;
77
import android.content.Context;
8-
import android.content.Intent;
9-
import android.content.IntentFilter;
108
import android.os.Bundle;
119
import android.util.Log;
1210
import android.content.pm.ApplicationInfo;
@@ -32,6 +30,12 @@
3230
import com.onesignal.OneSignal.EmailUpdateHandler;
3331
import com.onesignal.OneSignal.EmailUpdateError;
3432

33+
34+
import com.onesignal.OneSignal.NotificationOpenedHandler;
35+
import com.onesignal.OneSignal.NotificationReceivedHandler;
36+
import com.onesignal.OSNotificationOpenResult;
37+
import com.onesignal.OSNotification;
38+
3539
import org.json.JSONObject;
3640
import org.json.JSONArray;
3741
import org.json.JSONException;
@@ -40,16 +44,19 @@
4044
/**
4145
* Created by Avishay on 1/31/16.
4246
*/
43-
public class RNOneSignal extends ReactContextBaseJavaModule implements LifecycleEventListener {
44-
public static final String NOTIFICATION_OPENED_INTENT_FILTER = "GTNotificationOpened";
45-
public static final String NOTIFICATION_RECEIVED_INTENT_FILTER = "GTNotificationReceived";
47+
public class RNOneSignal extends ReactContextBaseJavaModule implements LifecycleEventListener, NotificationReceivedHandler, NotificationOpenedHandler {
4648
public static final String HIDDEN_MESSAGE_KEY = "hidden";
4749

4850
private ReactApplicationContext mReactApplicationContext;
4951
private ReactContext mReactContext;
5052
private boolean oneSignalInitDone;
5153
private boolean registeredEvents = false;
5254

55+
private OSNotificationOpenResult coldStartNotificationResult;
56+
private boolean setNotificationOpenedHandler = false;
57+
private boolean didSetRequiresPrivacyConsent = false;
58+
private boolean waitingForUserPrivacyConsent = false;
59+
5360
//ensure only one callback exists at a given time due to react-native restriction
5461
private Callback pendingGetTagsCallback;
5562

@@ -81,19 +88,12 @@ private void initOneSignal() {
8188
// Uncomment to debug init issues.
8289
// OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.ERROR);
8390

84-
if (!registeredEvents) {
85-
registeredEvents = true;
86-
registerNotificationsOpenedNotification();
87-
registerNotificationsReceivedNotification();
88-
}
89-
9091
OneSignal.sdkType = "react";
9192

9293
String appId = appIdFromManifest(mReactApplicationContext);
9394

94-
if (appId != null && appId.length() > 0) {
95+
if (appId != null && appId.length() > 0)
9596
init(appId);
96-
}
9797
}
9898

9999
private void sendEvent(String eventName, Object params) {
@@ -125,12 +125,10 @@ public void init(String appId) {
125125
context = mReactApplicationContext.getApplicationContext();
126126
}
127127

128-
OneSignal.init(context,
129-
null,
130-
appId,
131-
new NotificationOpenedHandler(mReactContext),
132-
new NotificationReceivedHandler(mReactContext)
133-
);
128+
OneSignal.init(context, null, appId, this, this);
129+
130+
if (this.didSetRequiresPrivacyConsent)
131+
this.waitingForUserPrivacyConsent = true;
134132
}
135133

136134
@ReactMethod
@@ -389,41 +387,26 @@ public void userProvidedPrivacyConsent(Promise promise) {
389387
promise.resolve(OneSignal.userProvidedPrivacyConsent());
390388
}
391389

392-
private void registerNotificationsReceivedNotification() {
393-
IntentFilter intentFilter = new IntentFilter(NOTIFICATION_RECEIVED_INTENT_FILTER);
394-
mReactContext.registerReceiver(new BroadcastReceiver() {
395-
@Override
396-
public void onReceive(Context context, Intent intent) {
397-
notifyNotificationReceived(intent.getExtras());
398-
}
399-
}, intentFilter);
400-
}
401-
402-
private void registerNotificationsOpenedNotification() {
403-
IntentFilter intentFilter = new IntentFilter(NOTIFICATION_OPENED_INTENT_FILTER);
404-
mReactContext.registerReceiver(new BroadcastReceiver() {
405-
@Override
406-
public void onReceive(Context context, Intent intent) {
407-
notifyNotificationOpened(intent.getExtras());
408-
}
409-
}, intentFilter);
390+
@Override
391+
public void notificationReceived(OSNotification notification) {
392+
this.sendEvent("OneSignal-remoteNotificationReceived", RNUtils.jsonToWritableMap(notification.toJSONObject()));
410393
}
411394

412-
private void notifyNotificationReceived(Bundle bundle) {
413-
try {
414-
JSONObject jsonObject = new JSONObject(bundle.getString("notification"));
415-
sendEvent("OneSignal-remoteNotificationReceived", RNUtils.jsonToWritableMap(jsonObject));
416-
} catch(Throwable t) {
417-
t.printStackTrace();
395+
@Override
396+
public void notificationOpened(OSNotificationOpenResult result) {
397+
if (!this.setNotificationOpenedHandler) {
398+
this.coldStartNotificationResult = result;
399+
return;
418400
}
401+
this.sendEvent("OneSignal-remoteNotificationOpened", RNUtils.jsonToWritableMap(result.toJSONObject()));
419402
}
420403

421-
private void notifyNotificationOpened(Bundle bundle) {
422-
try {
423-
JSONObject jsonObject = new JSONObject(bundle.getString("result"));
424-
sendEvent("OneSignal-remoteNotificationOpened", RNUtils.jsonToWritableMap(jsonObject));
425-
} catch(Throwable t) {
426-
t.printStackTrace();
404+
@ReactMethod
405+
public void didSetNotificationOpenedHandler() {
406+
this.setNotificationOpenedHandler = true;
407+
if (this.coldStartNotificationResult != null) {
408+
this.notificationOpened(this.coldStartNotificationResult);
409+
this.coldStartNotificationResult = null;
427410
}
428411
}
429412

@@ -433,10 +416,7 @@ public String getName() {
433416
}
434417

435418
@Override
436-
public void onHostDestroy() {
437-
OneSignal.removeNotificationOpenedHandler();
438-
OneSignal.removeNotificationReceivedHandler();
439-
}
419+
public void onHostDestroy() { }
440420

441421
@Override
442422
public void onHostPause() {

index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function checkIfInitialized() {
5353
}
5454

5555
export default class OneSignal {
56-
static addEventListener(type: any, handler: Function) {
56+
static addEventListener(type, handler) {
5757
if (!checkIfInitialized()) return;
5858

5959
// Listen to events of notification received, opened, device registered and IDSAvailable.
@@ -65,6 +65,10 @@ export default class OneSignal {
6565

6666
_notificationHandler.set(type, handler);
6767

68+
if (type == 'opened') {
69+
RNOneSignal.didSetNotificationOpenedHandler();
70+
}
71+
6872
// Check if there is a cache for this type of event
6973
var cache = _notificationCache.get(type);
7074
if (handler && cache) {
@@ -102,7 +106,7 @@ export default class OneSignal {
102106
}
103107
}
104108

105-
static promptForPushNotificationsWithUserResponse(callback: Function) {
109+
static promptForPushNotificationsWithUserResponse(callback) {
106110
if (!checkIfInitialized()) return;
107111

108112
if (Platform.OS === 'ios') {
@@ -156,7 +160,7 @@ export default class OneSignal {
156160
}
157161
}
158162

159-
static checkPermissions(callback: Function) {
163+
static checkPermissions(callback) {
160164
if (!checkIfInitialized()) return;
161165

162166
if (Platform.OS === 'ios') {
@@ -180,7 +184,7 @@ export default class OneSignal {
180184
}
181185
}
182186

183-
static getPermissionSubscriptionState(callback: Function) {
187+
static getPermissionSubscriptionState(callback) {
184188
if (!checkIfInitialized()) return;
185189

186190
invariant(

0 commit comments

Comments
 (0)