react-native-notification foreground messages

1k Views Asked by At

Hi everyone

I use library wix/react-native-notifications

It looks like I do something wrong with my android (ios works fine). For me:

  • registerRemoteNotificationsRegistered
  • registerRemoteNotificationsRegistered
  • registerNotificationOpened
  • getInitialNotification

work good.

But:

  • registerNotificationReceivedForeground
  • registerNotificationReceivedBackground

have never triggered.

I think I put something wrong in AndroidManifest.xml (I have tried different variant but it still doesn't work).

Or problem occurs from using

  • react-native-notification
  • react-native-pusher-push-notifications

at the same time.

Please maybe someone have some ideas?

I use:

  • "react-native-notifications": "^3.2.2",
  • "react-native-pusher-push-notifications": "^2.4.0",
  • "react": "16.9.0",
  • "react-native": "0.61.5",

Part of AndroidManifest.xml that I think can be wrong:

<service
  android:name=".java.MyFirebaseMessagingService"
  android:exported="false"
>
  <intent-filter>
    <action android:name="com.google.firebase.MESSAGING_EVENT"/>
  </intent-filter>
</service>
2

There are 2 best solutions below

0
On

You can receive Push Notification on foreground in react native in this way

import {firebase} from '@react-native-firebase/messaging'
import PushNotification from "react-native-push-notification";

 useEffect(()=>{notification()},[])

const notification = () => {
    const messaging=firebase.messaging;
    const unsubscribe = messaging().onMessage(async remoteMessage => {
      console.log('A new FCM message arrived!', JSON.stringify(remoteMessage));

        PushNotification.localNotification({
            channelId: "reminder",
            message: remoteMessage.notification.body,
            title: remoteMessage.notification.title,
            bigPictureUrl: remoteMessage.notification.android.imageUrl,
            smallIcon: remoteMessage.notification.android.imageUrl,
        });
    });
    return unsubscribe;
}
1
On

Ok, maybe it will be helpful for someone or just start some discussions )

Well, from my understanding the react-native-pusher-push-notifications handle notification first, so library react-native-notifications can't catch that.

Not the better decision but it will works before I find better solution.

I modified file PusherWrapper.java in react-native-pusher-push-notifications library to handle more information. The main problem was that library doesn't return info that send in data part of notification. enter image description here