invertase / react-native-notifee

Moved to https://github.com/invertase/notifee
https://invertase.io/blog/open-sourcing-notifee
Other
466 stars 31 forks source link

[iOS] remote notification: no custom data received when app is in background/terimnated #347

Closed fabyeah closed 3 years ago

fabyeah commented 3 years ago

When the app is in foreground, everything works. But when the app is backgrounded/terminated, only title and body fields are received from a remote push notification.

This is the apns field for the notification in the firebase cloud function:

apns: {
      headers: {
        'apns-expiration': String(expirationDate),
      },
      payload: {
        aps: {
          // Important, to receive `onMessage` event in the foreground when message is incoming
          'content-available': 1,
          // Important, without this the Notifee notification extension won't fire
          'mutable-content': 1,
          alert: {
            title: notification.title,
            subtitle: notification.subtitle,
            body: notification.text,
          },
        },
        url: notification.url,
        ...notification.customData, // customData1 and customData2 fields
        notifee_options: {
          // URL to pointing to a remote image
          // image: notification.imageUrl,
          // ios: {
          //   // A local sound file you have inside your app's bundle
          //   sound: 'media/kick.wav',
          //   // A category that's already been created by your app
          //   categoryId: 'post',
          //   // any other api properties for NotificationIOS
          // },
        },
      },
    },

In my RN app I have this event handler:

useEffect(() => {
    // Events when app is active / in the foreground.
    return notifee.onForegroundEvent(async ({ type, detail }) => {
      console.log('notifee.onForegroundEvent');
      console.log('detail:', JSON.stringify(detail));
      if (type === EventType.PRESS) {
        await Linking.openURL(detail!.notification!.data!.url);
      }
    });
  }, []);

and this for foreground messages:

  useEffect(() => {
    // Triggers when notification is received while app is in foreground.
    const unsubscribe = messaging().onMessage(async (remoteMessage) => {
      console.log('onMessage:', JSON.stringify(remoteMessage));
      displayNotification(remoteMessage);
    });

If the app was in foreground, I get this console.log for remoteMessage:

onMessage: {
  "messageId":"1627598898806161",
  "data": {
    "customData1":"foo",
    "customData2":"bar",
    "url":"https://myurl.com",
    "notifee_options": {},
  }
  "contentAvailable":true,
  "mutableContent":true,
  "notification": {
    "body":"This is the body",
    "ios": {
      "subtitle":"This is the subtitle"
    },
    "title":"🔒 Silbergold"
  }
}

Note that all my custom fields are there.

If the app was in background/terminated, when the notification is pressed, the app is opened, the handler runs and I get this console.log for detail:

detail: {
  "pressAction": {
    "id":"default"
  },
  "notification": {
    "body":"This is the body",
    "data":{},
    "title":"This is the title"
  }
}

Note that all my custom fields are missing, as well as subtitle. And as there is no url, I now get an error.

On top of this, I'm certain that everything worked fine like this. But I had to rename the app now, which was a ton of work and changes in itunes connect, so I can't check out an old commit to compare. But I never ran into this error before. I feel like I'm going crazy. Tried reinstalling Notifee, cleaning Xcode, etc. the issue is still there.

Now I found out, that whatever fields I set inside notifee_options will be available in the detail.notification, next to title, body and data. This way I can make it work again. So is this actually the correct way to pass custom fields? But why is data empty?

As a side note, I also have messaging().onNotificationOpenedApp and messaging().getInitialNotification() in my code for iOS, next to messaging().onMessage. But those never get called....? I feel like something is broken here?

helenaford commented 3 years ago

hey, thanks for the report.

There are plans on our roadmap to populate the data object outside of notifee_options but currently, you need to set data inside notifee_options as you mentioned. This is planned for our next release to forward any custom keys outside of notifee_options as data.

For the events, any notification that is displayed by notifee will be handled by notifee. So when the notification is displayed by the notifications service extension, onForegroundEvent should be triggered when the app first opens.

Hope that helps and answers your questions.

carlos187-dev commented 3 years ago

I have the same problem, but I'm using Amazon Pinpoint service . The notification fires but without sound and all the notifee properties. My question is if is there a way to work with amazon Pinpoint and notifee?. Thanks

fabyeah commented 3 years ago

@helenaford thanks for clarifying. The notification was displayed by the notifications service extension. I guess one can't modify the notification in the Javascript in that case, unfortunately, but only in the AppDelegate.m file?