Skip to content

Instantly share code, notes, and snippets.

@monotv
Created March 24, 2020 22:39
Show Gist options
  • Save monotv/d6a064a1af27524c520689ada5ffe29f to your computer and use it in GitHub Desktop.
Save monotv/d6a064a1af27524c520689ada5ffe29f to your computer and use it in GitHub Desktop.
Patch to get foreground notifications on Android working
diff --git a/node_modules/react-native-notifications/lib/android/app/src/main/AndroidManifest.xml b/node_modules/react-native-notifications/lib/android/app/src/main/AndroidManifest.xml
index 7053040..a8bc654 100644
--- a/node_modules/react-native-notifications/lib/android/app/src/main/AndroidManifest.xml
+++ b/node_modules/react-native-notifications/lib/android/app/src/main/AndroidManifest.xml
@@ -11,25 +11,11 @@
android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
-
- <application>
-
- <!--
- A proxy-service that gives the library an opportunity to do some work before launching/resuming the actual application task.
- -->
- <service android:name=".core.ProxyService"/>
-
- <service
- android:name=".fcm.FcmInstanceIdListenerService">
- <intent-filter>
- <action android:name="com.google.firebase.MESSAGING_EVENT" />
- <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
- </intent-filter>
- </service>
-
- <service
- android:name=".fcm.FcmInstanceIdRefreshHandlerService"
- android:exported="false" />
- </application>
-
+ <!--
+ TODO:
+ These lines were moved to the app manifest because otherwise
+ we would not be able to see notifications with app in foreground.
+ We either need to look for a different or maybe custom solution or
+ wait for a fix in this library.
+ -->
</manifest>
diff --git a/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java b/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java
index 0140e8c..bb2b7f1 100644
--- a/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java
+++ b/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java
@@ -153,8 +153,8 @@ public class PushNotification implements IPushNotification {
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
-
- int resourceID = mContext.getResources().getIdentifier("notification_icon", "drawable", mContext.getPackageName());
+ // TODO: changed icon name to ic_notification to match our own name
+ int resourceID = mContext.getResources().getIdentifier("ic_notification", "drawable", mContext.getPackageName());
if (resourceID != 0) {
notification.setSmallIcon(resourceID);
} else {
@@ -164,7 +164,7 @@ public class PushNotification implements IPushNotification {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
CHANNEL_NAME,
- NotificationManager.IMPORTANCE_DEFAULT);
+ NotificationManager.IMPORTANCE_HIGH);
final NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
notification.setChannelId(CHANNEL_ID);
diff --git a/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotificationProps.java b/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotificationProps.java
index daab203..df2163f 100644
--- a/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotificationProps.java
+++ b/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotificationProps.java
@@ -10,12 +10,17 @@ public class PushNotificationProps {
mBundle = bundle;
}
+
+ /*
+ TODO: keys here were wrong and this returned null values, resulting in blank notifications
+ when the app was in foreground
+ */
public String getTitle() {
- return mBundle.getString("title");
+ return mBundle.getString("gcm.notification.title");
}
public String getBody() {
- return mBundle.getString("body");
+ return mBundle.getString("gcm.notification.body");
}
public Bundle asBundle() {
@aminerol
Copy link

aminerol commented Apr 7, 2020

You don't have to change the getTitle and getBody methods, to get those correctly you should update your json payload as below :

"data":{
     "title":"am title",
      "body":"am body",
}

the title and body also extra objects should be inside a data object

@monotv
Copy link
Author

monotv commented Apr 7, 2020

You don't have to change the getTitle and getBody methods, to get those correctly you should update your json payload as below :

"data":{
     "title":"am title",
      "body":"am body",
}

the title and body also extra objects should be inside a data object

I can not change the JSON payload because this comes from Firebase.

@kurroo10
Copy link

how to apply this patch actually ? @monotv

@monotv
Copy link
Author

monotv commented Apr 24, 2020

how to apply this patch actually ? @monotv

https://www.npmjs.com/package/patch-package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment