Skip to content

Commit

Permalink
fix(pushnotification): onTap of notification after the app is killed …
Browse files Browse the repository at this point in the history
…in Android Oreo or higher (aws-amplify#9729)

* fix: make the notification open intent explicit

* remove irrelavant gradle changes

Co-authored-by: Manoj NB <[email protected]>
Co-authored-by: Caleb Pollman <[email protected]>
  • Loading branch information
3 people authored Apr 5, 2022
1 parent 0b8f0f6 commit eaaa2c4
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 39 deletions.
10 changes: 1 addition & 9 deletions packages/pushnotification/__tests__/PushNotification-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,12 @@ describe('PushNotification:', () => {
'addEventListenerForAndroid'
);

const nativeInitSpy = jest.spyOn(
NativeModules.RNPushNotification,
'initialize'
);

const pushnotification = new PushNotification(null);
pushnotification.configure(defaultConfig);

expect.assertions(2);
expect.assertions(1);
expect(androidEventListenerSpy).toHaveBeenCalledTimes(3);
expect(nativeInitSpy).toHaveBeenCalledTimes(1);

androidEventListenerSpy.mockClear();
nativeInitSpy.mockClear();
});

test('should initialize iOS', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,17 @@

public class RNPushNotificationModule extends ReactContextBaseJavaModule {
private static final String LOG_TAG = "RNPushNotificationModule";
private boolean receiverRegistered;

public RNPushNotificationModule(ReactApplicationContext reactContext) {
super(reactContext);
Log.i(LOG_TAG, "constructing RNPushNotificationModule");
this.receiverRegistered = false;
}

@Override
public String getName() {
return "RNPushNotification";
}

@ReactMethod
public void initialize() {
ReactApplicationContext context = getReactApplicationContext();
Log.i(LOG_TAG, "initializing RNPushNotificationModule");
if (!this.receiverRegistered) {
this.receiverRegistered = true;
Log.i(LOG_TAG, "registering receiver");
Application applicationContext = (Application) context.getApplicationContext();
RNPushNotificationBroadcastReceiver receiver = new RNPushNotificationBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter("com.amazonaws.amplify.pushnotification.NOTIFICATION_OPENED");
applicationContext.registerReceiver(receiver, intentFilter);
}
}

@ReactMethod
public void getToken(final Callback onSuccessCallback, final Callback onErrorCallback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public class RNPushNotificationHelper {
private static final long ONE_HOUR = 60 * ONE_MINUTE;
private static final long ONE_DAY = 24 * ONE_HOUR;
private static final String LOG_TAG = "RNPushNotificationHelper";
private static final String NOTIFICATION_OPENED = "com.amazonaws.amplify.pushnotification.NOTIFICATION_OPENED";

public RNPushNotificationHelper(Application context) {
this.context = context;
Expand Down Expand Up @@ -257,7 +256,7 @@ public void sendToNotificationCentre(Bundle bundle) {

notification.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText));

Intent intent = new Intent(NOTIFICATION_OPENED);
Intent intent = new Intent(context, RNPushNotificationBroadcastReceiver.class);
intent.putExtra("notification", bundle);

Log.i(LOG_TAG, "sendNotification: " + intent);
Expand Down
21 changes: 8 additions & 13 deletions packages/pushnotification/src/PushNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ export default class PushNotification {
this._config = {};
}
this.updateEndpoint = this.updateEndpoint.bind(this);
this.handleNotificationReceived = this.handleNotificationReceived.bind(
this
);
this.handleNotificationReceived =
this.handleNotificationReceived.bind(this);
this.handleNotificationOpened = this.handleNotificationOpened.bind(this);
this._checkIfOpenedByNotification = this._checkIfOpenedByNotification.bind(
this
);
this._checkIfOpenedByNotification =
this._checkIfOpenedByNotification.bind(this);
this.addEventListenerForIOS = this.addEventListenerForIOS.bind(this);
this._currentState = AppState.currentState;
this._androidInitialized = false;
Expand Down Expand Up @@ -133,7 +131,6 @@ export default class PushNotification {
REMOTE_NOTIFICATION_RECEIVED,
this.handleNotificationReceived
);
RNPushNotification.initialize();

// check if the token is cached properly
if (!(await this._registerTokenCached())) {
Expand Down Expand Up @@ -254,9 +251,8 @@ export default class PushNotification {

handleNotificationReceived(rawMessage) {
logger.debug('handleNotificationReceived, raw data', rawMessage);
const { eventSource, eventSourceAttributes } = this.parseMessageData(
rawMessage
);
const { eventSource, eventSourceAttributes } =
this.parseMessageData(rawMessage);

if (!eventSource) {
logger.debug('message received is not from a pinpoint eventSource');
Expand Down Expand Up @@ -294,9 +290,8 @@ export default class PushNotification {
});

logger.debug('handleNotificationOpened, raw data', rawMessage);
const { eventSource, eventSourceAttributes } = this.parseMessageData(
rawMessage
);
const { eventSource, eventSourceAttributes } =
this.parseMessageData(rawMessage);

if (!eventSource) {
logger.debug('message received is not from a pinpoint eventSource');
Expand Down

0 comments on commit eaaa2c4

Please sign in to comment.