allboatsrise / expo-marketingcloudsdk

Expo module for Salesforce Marketing Cloud SDK
MIT License
12 stars 12 forks source link

Push notifications not showing on Android with the app closed #19

Closed fbonesso closed 1 year ago

fbonesso commented 1 year ago

Hello folks!

I'm not really sure if this is related to this library or expo-notifications itself but I'm having a hard time making it work on Android when the app is closed.

The push notification is delivered on SFMC, the device is registered, and I can see it right away after opening the app but for some reason, it doesn't show when the app is closed.

I was looking into the AndroidManifest.xml file for this plugin but everything looks correct to me.

I'm currently on SDK48, does anyone have any idea on what could be causing it?

Really appreciate it!

andrejpavlovic commented 1 year ago

And if the app is killed, does it show up?

Also, you might need something like this:

import * as Notifications from 'expo-notifications'
Notifications.setNotificationHandler({
  handleNotification: async (_notification) => {
    return {
      shouldShowAlert: true,
      shouldPlaySound: true,
      shouldSetBadge: true,
    }
  },
})
fbonesso commented 1 year ago

And if the app is killed, does it show up?

Also, you might need something like this:

import * as Notifications from 'expo-notifications'
Notifications.setNotificationHandler({
  handleNotification: async (_notification) => {
    return {
      shouldShowAlert: true,
      shouldPlaySound: true,
      shouldSetBadge: true,
    }
  },
})

Yes I have the setNotificationHandler in place as well on the top level. When the app is killed it doesn't show up on Android, iOS works fine for all scenarios.

andrejpavlovic commented 1 year ago

After hiding splash page, this is the kind of code we use to request notification permissions and configure marketing cloud sdk. Not sure what else I could suggest you look into.

import * as Notifications from 'expo-notifications'
import * as MarketingCloud from '@allboatsrise/expo-marketingcloudsdk'

useEffect(() => {
  if (!spashScreenHidden) return undefined

  let cleanup = () => {}

  ;(async () => {
    // request push notifications permission on load
    // TODO: show this elsewhere when it's more relevant
    let result = await Notifications.getPermissionsAsync()
    if (!result.granted && result.canAskAgain) {
      result = await Notifications.requestPermissionsAsync({
        ios: {
          allowAlert: true,
          allowBadge: true,
          allowSound: true,
        },
      })
    }

    if (!result.granted) return

    const token = await Notifications.getDevicePushTokenAsync()

    // let Marketing Cloud SDK the value of current push token
    MarketingCloud.setSystemToken(token.data)

    // In rare situations a push token may be changed by the push notification service while the app is running.
    const subscription = Notifications.addPushTokenListener((newToken) => {
      MarketingCloud.setSystemToken(newToken.data)
    })
    cleanup = () => subscription.remove()
  })()

  return () => cleanup()
}, [spashScreenHidden])
rodrix commented 1 year ago

Thanks for this thread, guys! I'm working on this right now!

FelipeGCastro commented 1 year ago

I'm also not receiving push notifications on Android when the app is closed. IOS works fine!

FelipeGCastro commented 1 year ago

@andrejpavlovic, looks like expo-marketingcloudsdk started SDK in onCreate from Activity, but on documentation says that needs to be on Application as you can see in the answer to this issue question: https://salesforce.stackexchange.com/questions/382120/cant-receive-notification-when-the-app-is-terminated-on-android

I tested locally and looks like background messages are arriving when SDK is initiated in Application onCreate using "createApplicationLifecycleListeners"

andrejpavlovic commented 1 year ago

@FelipeGCastro If you put together a PR I can review it and make a new build.

FelipeGCastro commented 1 year ago

@andrejpavlovic here is the PR.

andrejpavlovic commented 1 year ago

Fix published in [49.0.1](https://github.com/allboatsrise/expo-marketingcloudsdk/releases/tag/49.0.1

andrejpavlovic commented 1 year ago

Closing as it should be resolved in 49.0.1 - please re-open if issue persists.