-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sometimes App doesn't Open on android after click on notification #10118
Comments
It seems this occurs on Android 12 devices. Here is a reference of the issue: invertase/notifee#250 The amplify android code currently relies on a notification trampoline which is no longer valid for android 12 devices. it silently errors but one can see the error using The recommendation now is to use a Inside your I'm working on doing this now and can post a more complete example later. |
Here is the patch I am using to make the push notifications module compatible with android 12 Note that the changes in the diff --git a/node_modules/@aws-amplify/pushnotification/android/src/main/java/com/amazonaws/amplify/pushnotification/RNPushNotificationMessagingService.java b/node_modules/@aws-amplify/pushnotification/android/src/main/java/com/amazonaws/amplify/pushnotification/RNPushNotificationMessagingService.java
index 1057d67..7fdc8e9 100644
--- a/node_modules/@aws-amplify/pushnotification/android/src/main/java/com/amazonaws/amplify/pushnotification/RNPushNotificationMessagingService.java
+++ b/node_modules/@aws-amplify/pushnotification/android/src/main/java/com/amazonaws/amplify/pushnotification/RNPushNotificationMessagingService.java
@@ -104,12 +104,10 @@ public class RNPushNotificationMessagingService extends FirebaseMessagingService
Log.i(LOG_TAG, "sendNotification: " + bundle);
- if (!isForeground) {
- Application applicationContext = (Application) context.getApplicationContext();
- RNPushNotificationHelper pushNotificationHelper = new RNPushNotificationHelper(applicationContext);
- pushNotificationHelper.sendToNotificationCentre(bundle);
- }
-
+ // we want to send notification to center api regardless of if app in fg/bg
+ Application applicationContext = (Application) context.getApplicationContext();
+ RNPushNotificationHelper pushNotificationHelper = new RNPushNotificationHelper(applicationContext);
+ pushNotificationHelper.sendToNotificationCentre(bundle);
}
// whether the app is in foreground
diff --git a/node_modules/@aws-amplify/pushnotification/android/src/main/java/com/amazonaws/amplify/pushnotification/modules/RNPushNotificationBroadcastReceiver.java b/node_modules/@aws-amplify/pushnotification/android/src/main/java/com/amazonaws/amplify/pushnotification/modules/RNPushNotificationBroadcastReceiver.java
index 3f0a87f..041c140 100644
--- a/node_modules/@aws-amplify/pushnotification/android/src/main/java/com/amazonaws/amplify/pushnotification/modules/RNPushNotificationBroadcastReceiver.java
+++ b/node_modules/@aws-amplify/pushnotification/android/src/main/java/com/amazonaws/amplify/pushnotification/modules/RNPushNotificationBroadcastReceiver.java
@@ -15,6 +15,8 @@ package com.amazonaws.amplify.pushnotification.modules;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
+import android.net.Uri;
+import android.os.Bundle;
import android.util.Log;
import com.facebook.react.ReactApplication;
@@ -24,6 +26,9 @@ import com.facebook.react.bridge.ReactContext;
/**
* The Amazon Pinpoint push notification receiver.
+ *
+ * NOTE: this is no longer used due to android 12 req:
+ * https://developer.android.com/about/versions/12/behavior-changes-12#notification-trampoline-update-app
*/
public class RNPushNotificationBroadcastReceiver extends BroadcastReceiver {
@@ -33,6 +38,8 @@ public class RNPushNotificationBroadcastReceiver extends BroadcastReceiver {
String packageName = context.getPackageName();
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
String className = launchIntent.getComponent().getClassName();
+ Log.d(LOG_TAG, String.format("%s %s", packageName, className));
+
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
@@ -41,9 +48,15 @@ public class RNPushNotificationBroadcastReceiver extends BroadcastReceiver {
}
}
- private void openApp(Context context) {
+ private void openApp(Context context, Bundle notification) {
Class intentClass = getMainActivityClass(context);
- Intent launchIntent = new Intent(context, intentClass);
+ String deeplink = notification.getString("pinpoint.deeplink");
+ Boolean hasDeeplink = deeplink != null;
+
+ // if pinpoint data has a deeplink, we use an ACTION_VIEW intent (https://developer.android.com/reference/android/content/Intent#ACTION_VIEW)
+ // using this type of Intent is what native android Linking.getInitialUrl expects (https://github.com/facebook/react-native/blob/v0.64.3/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java#L59)
+ // this allows us to handle deep linking on cold push
+ Intent launchIntent = hasDeeplink ? new Intent(Intent.ACTION_VIEW, Uri.parse(deeplink)) : new Intent(context, intentClass);
if (launchIntent == null) {
Log.e(LOG_TAG, "Couldn't get app launch intent for campaign notification.");
return;
@@ -51,6 +64,7 @@ public class RNPushNotificationBroadcastReceiver extends BroadcastReceiver {
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
launchIntent.setPackage(null);
+ Log.e(LOG_TAG, "opening app");
context.startActivity(launchIntent);
}
@@ -77,7 +91,7 @@ public class RNPushNotificationBroadcastReceiver extends BroadcastReceiver {
mReactInstanceManager.createReactContextInBackground();
}
}
- openApp(context);
+ openApp(context, intent.getBundleExtra("notification"));
}
private void emitNotificationOpenedEvent(ReactContext reactContext, Intent intent){
diff --git a/node_modules/@aws-amplify/pushnotification/android/src/main/java/com/amazonaws/amplify/pushnotification/modules/RNPushNotificationHelper.java b/node_modules/@aws-amplify/pushnotification/android/src/main/java/com/amazonaws/amplify/pushnotification/modules/RNPushNotificationHelper.java
index 8f3f97f..dc6d361 100644
--- a/node_modules/@aws-amplify/pushnotification/android/src/main/java/com/amazonaws/amplify/pushnotification/modules/RNPushNotificationHelper.java
+++ b/node_modules/@aws-amplify/pushnotification/android/src/main/java/com/amazonaws/amplify/pushnotification/modules/RNPushNotificationHelper.java
@@ -78,7 +78,7 @@ public class RNPushNotificationHelper {
notificationIntent.putExtra(RNPushNotificationPublisher.NOTIFICATION_ID, notificationID);
notificationIntent.putExtras(bundle);
- return PendingIntent.getBroadcast(context, notificationID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+ return PendingIntent.getBroadcast(context, notificationID, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
}
public void sendNotificationScheduled(Bundle bundle) {
@@ -221,7 +221,8 @@ public class RNPushNotificationHelper {
if (smallIcon != null) {
smallIconResId = res.getIdentifier(smallIcon, "mipmap", packageName);
} else {
- smallIconResId = res.getIdentifier("ic_notification", "mipmap", packageName);
+ // our default notification icon is in drawable dir
+ smallIconResId = res.getIdentifier("ic_notification", "drawable", packageName);
}
if (smallIconResId == 0) {
@@ -245,6 +246,7 @@ public class RNPushNotificationHelper {
}
notification.setSmallIcon(smallIconResId);
+ notification.setColor(Color.BLACK); // change notification icon color to black
String bigText = bundle.getString("bigText");
if (bigText == null) {
@@ -254,8 +256,21 @@ public class RNPushNotificationHelper {
notification.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText));
- Intent intent = new Intent(context, RNPushNotificationBroadcastReceiver.class);
+ String deeplink = bundle.getString("pinpoint.deeplink");
+ Boolean hasDeeplink = deeplink != null;
+ // if pinpoint data has a deeplink, we use an ACTION_VIEW intent (https://developer.android.com/reference/android/content/Intent#ACTION_VIEW)
+ // using this type of Intent is what native android Linking.getInitialUrl expects (https://github.com/facebook/react-native/blob/v0.64.3/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java#L59)
+ // this allows us to handle deep linking on cold push
+ Intent intent = hasDeeplink ? new Intent(Intent.ACTION_VIEW, Uri.parse(deeplink)) : context.getPackageManager().getLaunchIntentForPackage(packageName);
intent.putExtra("notification", bundle);
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+
+ int notificationID = Integer.parseInt(notificationIdString);
+
+ PendingIntent pendingIntent = PendingIntent.getActivity(context, notificationID, intent,
+ PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
+
+ notification.setContentIntent(pendingIntent);
Log.i(LOG_TAG, "sendNotification: " + intent);
@@ -311,15 +326,6 @@ public class RNPushNotificationHelper {
}
}
- int notificationID = Integer.parseInt(notificationIdString);
-
- // PendingIntent pendingIntent = PendingIntent.getActivity(context, notificationID, intent,
- // PendingIntent.FLAG_UPDATE_CURRENT);
- PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationID, intent,
- PendingIntent.FLAG_UPDATE_CURRENT);
-
- notification.setContentIntent(pendingIntent);
-
if (!bundle.containsKey("vibrate") || bundle.getBoolean("vibrate")) {
long vibration = bundle.containsKey("vibration") ? (long) bundle.getDouble("vibration") : DEFAULT_VIBRATION;
if (vibration == 0)
@@ -354,7 +360,7 @@ public class RNPushNotificationHelper {
bundle.putString("action", action);
actionIntent.putExtra("notification", bundle);
PendingIntent pendingActionIntent = PendingIntent.getBroadcast(context, notificationID, actionIntent,
- PendingIntent.FLAG_UPDATE_CURRENT);
+ PendingIntent.FLAG_IMMUTABLE);
notification.addAction(icon, action, pendingActionIntent);
}
}
I totally forgot to add this... Here is what that looks like package com.your.package
import android.content.Intent
import expo.modules.ReactActivityDelegateWrapper
import android.os.Bundle
import android.util.Log
import com.amazonaws.amplify.pushnotification.modules.RNPushNotificationJsDelivery
import com.facebook.react.*
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContext
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView
class MainActivity : ReactActivity() {
companion object {
val LOG_TAG = "com.your.package"
}
override fun getMainComponentName() = "package"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d(LOG_TAG, "onCreate")
// handles cold starts
handleNotificationOnIntent(intent)
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
this.intent = intent
Log.d(LOG_TAG, "onNewIntent")
// handles warm starts
handleNotificationOnIntent(intent)
}
private fun handleNotificationOnIntent(intent: Intent?) {
val notification = intent?.getBundleExtra("notification")
if (notification != null) {
// send the message to device emitter
// Construct and load our normal React JS code bundle
val mReactInstanceManager =
(this.applicationContext as ReactApplication).reactNativeHost.reactInstanceManager
val reactContext = mReactInstanceManager.currentReactContext
if (reactContext != null) {
Log.d(LOG_TAG, "emitting notification opened event")
val jsDelivery =
RNPushNotificationJsDelivery(reactContext as ReactApplicationContext?)
jsDelivery.emitNotificationOpened(notification)
} else {
Log.e(LOG_TAG, "failed to emit notification opened event due to no react context")
}
}
}
} The key is |
hi 👋 @JosephVasse |
Hello, i've never been able to reproduce it. However the Google Play console crash reports still reports this bug. |
@ChrisLFieldsII Would you please advise on you patch. I'm trying to implement it in my app in order to get PN work on Android 12+.
We passing the Thanks in advance! |
Hmm sounds like maybe your It's this api though: https://developer.android.com/reference/android/app/Activity#getIntent() So if you are using java, you might need to change thats the first thought that comes to my mind, just a difference between |
The patch works fine for me. Thanks @ChrisLFieldsII. Here is the Java implementation: package com.myapp;
/**PATCH*/
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactRootView;
import android.content.Intent;
import android.os.Bundle;
import com.amazonaws.amplify.pushnotification.modules.RNPushNotificationJsDelivery;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
public class MainActivity extends ReactActivity {
/**
* Returns the name of the main component registered from JavaScript. This is used to schedule
* rendering of the component.
*/
@Override
protected String getMainComponentName() {
return "MyApp";
}
/**
* Returns the instance of the {@link ReactActivityDelegate}. There the RootView is created and
* you can specify the renderer you wish to use - the new renderer (Fabric) or the old renderer
* (Paper).
*/
@Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new MainActivityDelegate(this, getMainComponentName());
}
public static class MainActivityDelegate extends ReactActivityDelegate {
public MainActivityDelegate(ReactActivity activity, String mainComponentName) {
super(activity, mainComponentName);
}
@Override
protected ReactRootView createRootView() {
ReactRootView reactRootView = new ReactRootView(getContext());
// If you opted-in for the New Architecture, we enable the Fabric Renderer.
reactRootView.setIsFabric(BuildConfig.IS_NEW_ARCHITECTURE_ENABLED);
return reactRootView;
}
@Override
protected boolean isConcurrentRootEnabled() {
// If you opted-in for the New Architecture, we enable Concurrent Root (i.e. React 18).
// More on this on https://reactjs.org/blog/2022/03/29/react-v18.html
return BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
}
}
/**
* React navigation - https://reactnavigation.org/docs/getting-started/
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(null);
// handles cold starts
handleNotificationOnIntent(getIntent());
}
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
// handles warm starts
handleNotificationOnIntent(intent);
}
private void handleNotificationOnIntent(Intent intent) {
if (intent == null) {
return;
}
Bundle notification = intent.getBundleExtra("notification");
if (notification != null) {
// send the message to device emitter
// Construct and load our normal React JS code bundle
ReactApplication applicationContext = (ReactApplication) getApplicationContext();
ReactInstanceManager mReactInstanceManager = applicationContext.getReactNativeHost().getReactInstanceManager();
ReactContext reactContext = mReactInstanceManager.getCurrentReactContext();
if (reactContext != null) {
RNPushNotificationJsDelivery jsDelivery =
new RNPushNotificationJsDelivery((ReactApplicationContext)reactContext);
jsDelivery.emitNotificationOpened(notification);
}
}
}
} |
@ChrisLFieldsII @iamaralinvestplay I've managed to pass this token to updateEndpoint but it was copy pasted from logcat and I've received push notification successfully. I'm trying to upgrade from 0.63.4 where everything working once, I'm upgrading to 0.67.5 it breaks. Many thanks for possible solution |
@LYevhen The team is actively working on a fix for Push Notifications on Android 12 with newer versions of React Native. Just a callout that this issue is not related to Android 12 and the original issue #10252 is, I realize the patch exists on this issue but just wanted to make that clear. For your other concern, are you saying that the |
@tannerabread Yes, You are right, onRegister callback is not working with Android device, but it is working with iOs device, I'm unable to migrate instantly to 0.70 + because of other modules that are not ready for RN starting from 0.68, so have to upgrade step by step. |
@LYevhen From my reproduction, it also seemed to work work React Native v0.67.5 and less without the patch applied as mentioned by @mtsymlov in the other issue. It was the upgrade to v0.68 that seemed to break the Push Notification category. If you are only having problems with the |
@tannerabread unfortunately it fails (rn 0.67.5 ) because of PendingIntent Flag in RNPushNotificationHelper without patch mentioned above, does not fail with patch, onRegister is not called at all.( 0.67.4 does not work so version 0.67.5 is special release from fb team targeted to fix android issue )
Is there a way to get this token directly from react native js code ?, so I can make a workaround |
Hi @JosephVasse we have recently released a rewritten Push Notification category with new features, can you try it out and let me know if it works for you? You can follow the migration steps here or if you are starting again from scratch follow the getting-started guide here Also note the API changes here for interacting with notifications |
Hi 👋 Closing this as resolved. This should be handled with the recent release of the rewritten Push Notification category mentioned above. If you are still experiencing this issue and in need of assistance, please feel free to comment and provide us with additional information so we can re-open this issue and be better able to assist you. Thank you! |
@tannerabread |
@ChrisLFieldsII the patch worked for me previously but from past few days everything is working fine as i am able to console data when onNotification is triggered but the pop up of the notification is not coming up i.e on console i m getting the notifications but visually i am not getting it on emulator |
Before opening, please confirm:
JavaScript Framework
React Native
Amplify APIs
Analytics, Push Notifications
Amplify Categories
notifications
Environment information
Describe the bug
Sometimes when a user receives a notification, clicking on it does nothing even though it is supposed to open app.
This is not really frequent hovewer we are trying to solve this issue.
Expected behavior
The application should open, using a deep link or just starting its normal way
Reproduction steps
As i said this bug is really infrequent,
i beleive it happens only on certains models (see below)
i haven't been able to reproduce it on my devices.
video-1658916897.mp4
Code Snippet
// Put your code below this line.
Log output
Here are the logs that google play console provides me :
aws-exports.js
Manual configuration
No response
Additional configuration
No response
Mobile Device
From google play console :
Huawei HUAWEI P smart 2020
Huawei P20
Huawei Y9 Prime 2019
Samsung Galaxy Note3
(Keep in mind that other users may have not shared crash infos)
Mobile Operating System
Android 5.0, 9 & 10
(Keep in mind that other users may have not shared crash infos)
The text was updated successfully, but these errors were encountered: