Skip to content

Commit 44b6650

Browse files
authored
Merge pull request #680 from geektimecoil/fix_intent_filter
Remove Intent-Filter Usage
2 parents 54a4d45 + 9761072 commit 44b6650

File tree

4 files changed

+41
-173
lines changed

4 files changed

+41
-173
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 & 63 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,51 +387,26 @@ public void userProvidedPrivacyConsent(Promise promise) {
389387
promise.resolve(OneSignal.userProvidedPrivacyConsent());
390388
}
391389

392-
@ReactMethod
393-
public void setExternalUserId(String externalId) {
394-
OneSignal.setExternalUserId(externalId);
395-
}
396-
397-
@ReactMethod
398-
public void removeExternalUserId() {
399-
OneSignal.removeExternalUserId();
400-
}
401-
402-
private void registerNotificationsReceivedNotification() {
403-
IntentFilter intentFilter = new IntentFilter(NOTIFICATION_RECEIVED_INTENT_FILTER);
404-
mReactContext.registerReceiver(new BroadcastReceiver() {
405-
@Override
406-
public void onReceive(Context context, Intent intent) {
407-
notifyNotificationReceived(intent.getExtras());
408-
}
409-
}, intentFilter);
410-
}
411-
412-
private void registerNotificationsOpenedNotification() {
413-
IntentFilter intentFilter = new IntentFilter(NOTIFICATION_OPENED_INTENT_FILTER);
414-
mReactContext.registerReceiver(new BroadcastReceiver() {
415-
@Override
416-
public void onReceive(Context context, Intent intent) {
417-
notifyNotificationOpened(intent.getExtras());
418-
}
419-
}, intentFilter);
390+
@Override
391+
public void notificationReceived(OSNotification notification) {
392+
this.sendEvent("OneSignal-remoteNotificationReceived", RNUtils.jsonToWritableMap(notification.toJSONObject()));
420393
}
421394

422-
private void notifyNotificationReceived(Bundle bundle) {
423-
try {
424-
JSONObject jsonObject = new JSONObject(bundle.getString("notification"));
425-
sendEvent("OneSignal-remoteNotificationReceived", RNUtils.jsonToWritableMap(jsonObject));
426-
} catch(Throwable t) {
427-
t.printStackTrace();
395+
@Override
396+
public void notificationOpened(OSNotificationOpenResult result) {
397+
if (!this.setNotificationOpenedHandler) {
398+
this.coldStartNotificationResult = result;
399+
return;
428400
}
401+
this.sendEvent("OneSignal-remoteNotificationOpened", RNUtils.jsonToWritableMap(result.toJSONObject()));
429402
}
430403

431-
private void notifyNotificationOpened(Bundle bundle) {
432-
try {
433-
JSONObject jsonObject = new JSONObject(bundle.getString("result"));
434-
sendEvent("OneSignal-remoteNotificationOpened", RNUtils.jsonToWritableMap(jsonObject));
435-
} catch(Throwable t) {
436-
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;
437410
}
438411
}
439412

@@ -443,10 +416,7 @@ public String getName() {
443416
}
444417

445418
@Override
446-
public void onHostDestroy() {
447-
OneSignal.removeNotificationOpenedHandler();
448-
OneSignal.removeNotificationReceivedHandler();
449-
}
419+
public void onHostDestroy() { }
450420

451421
@Override
452422
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)