Skip to content
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

Notifee notifications do not display for FCM data only message #1157

Open
kamprasad opened this issue Dec 8, 2024 · 4 comments
Open

Notifee notifications do not display for FCM data only message #1157

kamprasad opened this issue Dec 8, 2024 · 4 comments

Comments

@kamprasad
Copy link

Hello,

Im trying to display Notifee notification for react native app when app is on background or quit state. For this I use FCM data message only.

But notifications are not display on device level even tho data message receive on setBackgroundMessageHandler. Could someone help on this please ?

build.gradle

    buildToolsVersion = "34.0.0"
    minSdkVersion = 23
    compileSdkVersion = 34
    targetSdkVersion = 34
    ndkVersion = "26.1.10909125"
    kotlinVersion = "1.9.22"

Packages version

"react-native": "0.74.1",
"@notifee/react-native": "^9.1.3",
"@react-native-firebase/app": "^21.4.0",
"@react-native-firebase/messaging": "^21.4.0",

indexj.js code and payload received on setBackgroundMessageHandler as below.

indexj.js


 notifee.onBackgroundEvent(async ({type, detail}) => {
  const {notification, pressAction} = detail;
  const action_ids = [];

  if (type === EventType.ACTION_PRESS) {
    if (action_ids.includes(pressAction.id)) {
      // TODO - Make API Call
    }

    if (pressAction.id === 'open_in_chat') {
      
    }
  }
  await notifee.cancelNotification(notification.id);
});

messaging().setBackgroundMessageHandler(async remoteMessage => {

  await notifee.createChannel({
    id: 'default',
    name: 'default channel',
    vibration: true,
    importance: AndroidImportance.HIGH,
    vibrationPattern: [300, 500],
  });

  // For Android Notifications
  if (Platform.OS === 'android') {
    try {
      await notifee.displayNotification({
        title: remoteMessage.data?.title,
        body: remoteMessage.data?.body,
        android: {
          channelId: 'default',
          actions: [],
        },
      });
    } catch (error) {
      console.error('Notification Error:', error);
    }
  }
});

AppRegistry.registerComponent(appName, () => App);

FCM payload received as callback function param

{"data": {"body": "string", "date_time": "1733696313.0459878", "id": "a0c933e4-a847-41c1-950b-cff8a503ca16", "meta_data": "{\"actions\": []}", "title": "string"}, "from": "134145055004", "messageId": "0:1733696313204029%dc9bb158f9fd7ecd", "originalPriority": 1, "priority": 1, "sentTime": 1733696313197, "ttl": 2419200}

FCM server code


 message = messaging.Message(
        android=messaging.AndroidConfig(
            ttl=2419200,
            priority="high",
        ),
        webpush=messaging.WebpushConfig(
            headers={
                "TTL": "2419200",
                "Urgency": "high",
            },
        ),
        apns=messaging.APNSConfig(
            headers={
                "apns-expiration": (2419200),
                "apns-priority": "10",
            },
            payload=messaging.APNSPayload(
                aps=messaging.Aps(content_available=True)
            ),
        ),
        data={
            "title": "",
            "body": "",
            "id": "",
            "date_time": "",
            "meta_data": ""
        },
        token=token,
    )

try:
    response = messaging.send(message)
    print("Successfully sent message:", response)
    return True
except Error as e:
    print(f"Failed to send message: {e}")
    return False

AndroidManifest.xml


<manifest xmlns:android="http://schemas.android.com/apk/res/android">

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.RECORD_AUDIO" />
  <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
  <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
  <uses-permission android:name="android.permission.VIBRATE" />
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

  <application
    android:name=".MainApplication"
    android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher"
    android:usesCleartextTraffic="true"
    android:allowBackup="false"
    android:theme="@style/AppTheme">
    <activity
      android:name=".MainActivity"
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
      android:launchMode="singleTask"
      android:windowSoftInputMode="adjustResize"
      android:exported="true">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>

      <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
          android:scheme="twin" />
      </intent-filter>
    </activity>
  </application>
</manifest>
@kamprasad kamprasad changed the title Notifee notifications do no display for FCM data only message Notifee notifications do not display for FCM data only message Dec 8, 2024
@mikehardy
Copy link
Collaborator

I dunno, if you're receiving the FCM and your code is executing but the notification isn't showing you likely have some sort of notification permissions state on the device that is unexpected.

You'll want to watch adb logcat of the device while troubleshooting with lots of console.log statements etc

@kamprasad
Copy link
Author

kamprasad commented Dec 9, 2024

If I call notifee displayNotification func outside of setBackgroundMessageHandler callback function, i can see alert appear when the app start time. Seems like can't say that permission issue tho, i have tried same code in emulator and real device. Same behaviour. Do you have any other suggestion to look over?

@mikehardy
Copy link
Collaborator

I made a suggestion that doesn't seem to have been tried

You'll want to watch adb logcat of the device while troubleshooting with lots of console.log statements etc

@kamprasad
Copy link
Author

yea I did, for android, Fabric has not been enabled, is that the case not to show notification ?

12-09 19:23:57.614  6350  6350 D RNFirebaseMsgReceiver: broadcast received for message
12-09 19:23:57.624  6350  6429 I ReactNativeJS: { originalPriority: 1,
12-09 19:23:57.624  6350  6429 I ReactNativeJS:   priority: 1,
12-09 19:23:57.624  6350  6429 I ReactNativeJS:   sentTime: 1733743438002,
12-09 19:23:57.624  6350  6429 I ReactNativeJS:   data: 
12-09 19:23:57.624  6350  6429 I ReactNativeJS:    { 
12-09 19:23:57.624  6350  6429 I ReactNativeJS:      body: 'string',
12-09 19:23:57.624  6350  6429 I ReactNativeJS:      id: '98871169',
12-09 19:23:57.624  6350  6429 I ReactNativeJS:      title: 'string',
12-09 19:23:57.624  6350  6429 I ReactNativeJS:      date_time: '1733743437.9473214',
12-09 19:23:57.624  6350  6429 I ReactNativeJS:   from: '134145055004',
12-09 19:23:57.624  6350  6429 I ReactNativeJS:   messageId: '0:23232323%dc9bb158f9fd7ecd',
12-09 19:23:57.624  6350  6429 I ReactNativeJS:   ttl: 2419200 }
12-09 19:23:57.663  6350  6350 D Notifee::getReactContext: we are NOT in bridgeless new architecture mode
12-09 19:23:57.663  6350  6350 D Notifee::getReactContext: Determining ReactContext using fallback method
12-09 19:23:57.663  6350  6350 D Notifee::getReactContext: we are NOT in bridgeless new architecture mode
12-09 19:23:57.663  6350  6350 D Notifee::getReactContext: Determining ReactContext using fallback method
12-09 19:23:57.665  6350  6502 I NOTIFEE : (NotificationManager): Removing notification with id blDSwBqtfVQF6PpY2hms
12-09 19:23:57.666  6350  6502 I NOTIFEE : (NotificationManager): Removing notification with id blDSwBqtfVQF6PpY2hms
12-09 19:23:57.874   521   521 I NotificationService: Cannot find enqueued record for key: 0|com.voiceassistant1|-1167383925|null|10190

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants