aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.42k stars 2.12k forks source link

PushNotification.onNotificationOpened is not triggered in android and IOS #7826

Closed kobagapu closed 3 years ago

kobagapu commented 3 years ago

PushNotification.onNotification(notification => { // console.log('push_notification received', notification) if (Platform.OS === 'ios') { notification.finish(PushNotificationIOS.FetchResult.NoData) } if (notification.foreground) { console.log('onNotification foreground', notification); } else { console.log('onNotification background or closed', notification); } // extract the data passed in the push notification const data = JSON.parse(notification.data['pinpoint.jsonBody']); console.log('onNotification data', data); })

PushNotification.onNotificationOpened(notification => { console.log('onNotificationOpened', notification); // extract the data passed in the push notification const data = JSON.parse(notification['pinpoint.jsonBody']); console.log('onNotificationOpened data', data); });

I testing in Physical device as well as emulator also

amhinson commented 3 years ago

Can you share some more information about your app:

kobagapu commented 3 years ago

/*.// Pacakage.json { "name": "", "version": "0.0.1", "private": true, "scripts": { "android": "react-native run-android", "ios": "react-native run-ios", "start": "react-native start", "test": "jest --detectOpenHandles", "prettier-fix": "prettier --write */.js", "prettier-check": "prettier --list-different */.js", "lint-fix": "eslint --fix .", "lint-check": "eslint .", "rename": "react-native-rename", "postinstall": "npx jetify" }, "dependencies": { "@aws-amplify/pushnotification": "^3.2.16", "@babel/helper-validator-identifier": "^7.9.5", "@react-native-community/async-storage": "^1.11.0", "@react-native-community/datetimepicker": "^3.0.9", "@react-native-community/netinfo": "^5.9.1", "@react-native-community/picker": "^1.6.1", "@react-native-community/push-notification-ios": "^1.4.1", "@react-native-community/toolbar-android": "0.1.0-rc.2", "@react-native-mapbox-gl/maps": "^8.1.0-rc.5", "@sentry/react-native": "^1.7.1", "amazon-cognito-identity-js": "^4.3.0", "aws-amplify": "3.0.22", "aws-amplify-react-native": "4.2.3", "aws-sdk": "^2.751.0", "axios": "^0.19.0", "buffer": "^5.6.0", "config": "^3.3.1", "lodash": "^4.17.15", "lottie-ios": "^3.1.8", "lottie-react-native": "^3.5.0", "moment": "^2.26.0", "native-base": "^2.13.14", "prop-types": "^15.7.2", "react": "16.9.0", "react-native": "0.61.5", "react-native-calendar-picker": "7.0.8", "react-native-calendars": "^1.265.0", "react-native-camera": "davidgovea/react-native-camera#standalone-mlkit", "react-native-cli": "^2.0.1", "react-native-device-info": "^7.3.1", "react-native-draggable": "^3.3.0", "react-native-dropdown-picker": "^3.7.1", "react-native-elements": "^2.0.4", "react-native-fix-image": "^2.1.0", "react-native-fs": "^2.16.6", "react-native-geolocation-service": "^5.0.0", "react-native-gesture-handler": "^1.3.0", "react-native-google-autocomplete": "^0.1.9", "react-native-image-crop-picker": "0.33.3", "react-native-image-picker": "^2.3.3", "react-native-image-size": "^1.1.3", "react-native-intl-phone-input": "^1.2.26", "react-native-modal": "^11.5.6", "react-native-modal-datetime-picker": "^9.1.0", "react-native-modal-dropdown": "^0.7.0", "react-native-phone-input": "^0.2.4", "react-native-picker-select": "^7.0.0", "react-native-render-html": "^5.0.1", "react-native-screens": "^2.8.0", "react-native-searchable-dropdown": "^1.1.3", "react-native-shapes": "^0.1.0", "react-native-simple-radio-button": "^2.7.4", "react-native-splash-screen": "^3.2.0", "react-native-tags-input": "^1.0.10", "react-native-vector-icons": "^7.0.0", "react-native-version-check": "^3.4.2", "react-native-webview": "^10.9.2", "react-navigation": "^3.11.2", "react-redux": "^6.0.1", "redux": "^4.0.5", "redux-logger": "^3.0.6", "redux-persist": "^5.10.0", "redux-saga": "^1.0.2", "redux-thunk": "^2.3.0", "reduxsauce": "^1.0.1", "util": "^0.12.3" }, "devDependencies": { "@babel/core": "^7.6.2", "@babel/runtime": "^7.6.2", "@react-native-community/eslint-config": "^0.0.5", "babel-jest": "^24.9.0", "eslint": "^6.5.1", "eslint-config-prettier": "^4.1.0", "eslint-config-standard": "^12.0.0", "eslint-plugin-import": "^2.16.0", "eslint-plugin-node": "^8.0.1", "eslint-plugin-prettier": "^3.0.1", "eslint-plugin-promise": "^4.0.1", "eslint-plugin-react": "^7.12.4", "eslint-plugin-react-native": "^3.6.0", "eslint-plugin-standard": "^4.0.0", "jest": "^24.9.0", "metro-react-native-babel-preset": "^0.56.0", "prettier": "^1.16.4", "react-native-rename": "^2.4.1", "react-test-renderer": "16.9.0" }, "jest": { "preset": "react-native", "setupFiles": [ "./node_modules/react-native-gesture-handler/jestSetup.js" ], "transformIgnorePatterns": [ "/node_modules/(?!react-native)/.+" ] } } *//

/ AppDeleagate.m /

/**

import "AppDelegate.h"

import <React/RCTBridge.h>

import <React/RCTBundleURLProvider.h>

import <React/RCTRootView.h>

import

import

@import Firebase; @implementation AppDelegate

// Required to register for notifications

@end

/*/ AppDelegate.h**

import <React/RCTBridgeDelegate.h>

import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate>

@property (nonatomic, strong) UIWindow *window;

@end

/*/ NotificationSetups.js**

import { Platform,NativeModules } from 'react-native' import Amplify, { Auth } from 'aws-amplify'; import AsyncStorage from '@react-native-community/async-storage' import PushNotification from '@aws-amplify/pushnotification'; import PushNotificationIOS from '@react-native-community/push-notification-ios'; import axios from 'axios'; import {jwtToken} from "../../Tools/UserSessionToken" import DeviceInfo from 'react-native-device-info'; import NavigationService from '../../../App/Services/NavigationService'

PushNotification.onNotificationOpened(notification => { if (notification.foreground) { console.log('NotificationOpenedforeground') } else {

} });

const initializePushNotification = async (data) => { const config1 = await AsyncStorage.getItem('@config') const process = JSON.parse(config1) const env = await AsyncStorage.getItem('@EnvironmentData') const envData = JSON.parse(env)

Amplify.configure({// configuring amplify to get current auth session Auth: { mandatorySignIn: false, region:process.REGION, userPoolId:process.USER_POOL_ID, identityPoolId:process.IDENTITY_POOL_ID, userPoolWebClientId:process.CLIENT_ID, }, PushNotification: { appId: process.PINPOINT_ID, requestIOSPermissions: true } });

if (Platform.OS === 'ios') { PushNotification.requestIOSPermissions() }

// In case PushNotification.onRegister didn't work let isTokenCreated = await AsyncStorage.getItem("@pushToken") PushNotification.onRegister(async token => { if(isTokenCreated && isTokenCreated === token){ console.log("ios- token already sent to server") }else{ console.log("pushNotification",token) postApiDeviceTokenRegister(token,data,envData,process) } })

NativeModules.RNPushNotification.getToken(async token => {
  if(isTokenCreated && isTokenCreated === token){
     console.log("android- token already sent to server")
  }else{
    console.log("pushNotification",token)
    postApiDeviceTokenRegister(token,data,envData,process)
  }
})

PushNotification.onNotification(notification => { // console.log('push_notification received', notification) if (Platform.OS === 'ios') { notification.finish(PushNotificationIOS.FetchResult.NoData) } // if (notification.foreground) { // when push notification occurs in foreground it will again get the lista and render ui // this._startLoading() // this.apiCall() // } })

};

const postApiDeviceTokenRegister = async(deviceToken,data,envData,process) => { const token = await jwtToken() let deviceUniqueId = await DeviceInfo.getUniqueId();

let payload = { airportId : data.code, userId : data.userId, deviceToken : deviceToken, deviceType : Platform.OS === "ios" ? 'ios' : 'android', id: deviceUniqueId, backend_id : envData.backend_id }

console.log(payload,process.envUrl+'/api/device/register/') axios({ method: 'post', url: process.envUrl+'/api/device/register/', data: payload, headers: { Authorization: 'Bearer ' + token } }).then((res)=>{ console.log("res",res); AsyncStorage.setItem("@pushToken",deviceToken) }) .catch((err)=>{ console.log("Error",err); }) return data;

}

export { initializePushNotification };

araujobarret commented 3 years ago

I'm facing the same issue with Android and iOS sending the notifications from AWS Pinpoint (didn't test sending from another way) onNotification works but onNotificationOpened doesn't.

React Native: 0.63.4 @aws-amplify/pushnotification: 4.0.2

brendonparker commented 3 years ago

Also running into this. I'm wondering if some of the constants are out-of-wack.

At least based on this source, it looks like it should be localNotificationReceived and not localNotification https://github.com/react-native-push-notification-ios/push-notification-ios/blob/master/js/index.js

Still looking...

brendonparker commented 3 years ago

Hrm, disregard my original hypothesis.

Not sure what is going on, but I can get it to work when I register the event handler myself against PushNotificationIOS.addEventListener...

PushNotificationIOS.addEventListener('localNotification', (notification) => {
  console.log('the notification is opened', notification);
});
brendonparker commented 3 years ago

At least in my scenario, I was able to track down that I needed to call:

PushNotification.configure(awsConfig);

After doing so, it appears to work as expected.

brendonparker commented 3 years ago

The root cause may be differences in the aws-amplify and @aws-amplify/pushnotification package versions. Make sure you're using the latest for both. Otherwise, I think the configuration/resolution in the aws-sdk will propagate to the @aws-amplify/pushnotification lib, causing it not to auto-config.

araujobarret commented 3 years ago

@brendonparker thanks for the heads up, calling the PushNotification.configure solved my issue!

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

chrisbonifacio commented 3 years ago

Hi đź‘‹ Closing this as we have not heard back from you. If you are still experiencing this issue and in need of assistance, please feel free to comment and provide us with any information previously requested by our team members so we can re-open this issue and be better able to assist you. Thank you!

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.