allboatsrise / expo-marketingcloudsdk

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

Example of how to use allboatsrise / expo-marketingcloudsdk #1

Closed andrejpavlovic closed 1 year ago

andrejpavlovic commented 1 year ago

Copied from private message

I tried to use it in my project, but unfortunately without success. Could you share with me a working example of the project that uses this plugin for receiving push notifications via salesforce? From my site, I can offer to create the PR to update the README of the plugin with a description of how to use it in the app. Problems that I'm facing:

I also thought that maybe I should use only a plugin without the SDK but after running the development build on my iPhone I receive the error: ""main" has not been registered", so I guess I will need it.

andrejpavlovic commented 1 year ago

I’ve been working on a rewrite of the entire plugin - it compiles, but I have yet to get it fully tested. The new version supports both, iOS and Android and uses the latest iOS/Android SDKs.

If you want to try it out install this NPM version: 47.0.0-android.26 and let me know how it goes. Branch: feature/android. Don't let the name fool you, it should support both OSs.

You can remove react-native-marketingcloudsdk dependency, it's no longer needed by the new version.

In my case, the plugin configuration in app.config.ts looks something like this:

import { MarketingCloudSdkPluginProps } from '@allboatsrise/expo-marketingcloudsdk/plugin/build/types'

[
  '@allboatsrise/expo-marketingcloudsdk',
  {
    appId: process.env.SFMC_APP_ID,
    accessToken: process.env.SFMC_ACCESS_TOKEN,
    analyticsEnabled: false,
    applicationControlsBadging: false, 
    inboxEnabled: true,
    serverUrl: process.env.SFMC_SERVER_URL,
    iconFile: path.join(__dirname, 'assets', 'notification-icon.png'),
  } as MarketingCloudSdkPluginProps,
],

You need to allow notifications and then set the token. E.g

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

useEffect(() => {
  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()
}, [])

I guess optionally set contact key at some point when user logs in:

MarketingCloud.setContactKey(contactKey)

Logging is your friend:

MarketingCloud.addLogListener(data => ...)

Thanks for trying out it out!

MarchewkaMatthew commented 1 year ago

Hi Andrej,

Thanks for the detailed instruction on how we can start using the plugin. After updating the version to the: 47.0.0-android.26 I'm facing the issue in the eas build (ss in the attached image). I can see that on the feature/android branch you have fixed it with the commit "fix build". Could you publish the new npm package version with these changes? :)

image
andrejpavlovic commented 1 year ago

@MarchewkaMatthew Try now, I've published latest code under 47.0.0-android.27

MarchewkaMatthew commented 1 year ago

Thanks for the quick response @andrejpavlovic! After updating the version and setting the SystemToken I can finally see my device in the Salesforce MarketingCloud contacts. My contact has the status "Opted In" and Opt-in source "MOBILE_OPT_IN".

I was trying to test sending the notification from the Salesforce dashboard but unfortunately, the message has the error status. I've contacted salesforce support but they listed multiple possible reasons, very generic ones so it was not that helpful.

image

My wild guess is that it might be caused by the type of app I'm running on my phone. It's not the published app downloaded from the AppStore, but "Development build" created via eas build with this setup:

    "development": {
      "developmentClient": true,
      "distribution": "internal",
      "ios": {
        "resourceClass": "m-medium"
      }
    },

Have you tried sending the notifications to the development build app? Do you know if it's possible? I think that APNs allow sending notifications to this kind of apps, because when I use the expo notifications service, everything works as expected.

andrejpavlovic commented 1 year ago

@MarchewkaMatthew I seem to have the same issue. I'm able to receive the notification when I build it for the app store and install via test-flight, but if I build it locally via XCode, I can't get it to trigger.

I don't have direct access to the Marketing Cloud MobilePush platform, but I'm curious in your case, did you set the APN environment for MobilePush app to Development or Production? And did you set the right keys?

More info here: https://salesforce-marketingcloud.github.io/MarketingCloudSDK-iOS/get-started/get-started-setupapps.html 2023-03-27_215223

I'll try to get a dev app setup so I can test this myself, but it'll take some time.

MarchewkaMatthew commented 1 year ago

@andrejpavlovic unless I made a mistake while configuring the Salesforce Marketing Cloud MobilePush everything should be set to the "development" environment. I also set the same sandbox APNs p8 key as for the expo setup (while eas build).

I will recreate my setup, and if it not gonna help I will try to deliver my app to the TestFlight as soon as possible. I will let you know about the result.

MarchewkaMatthew commented 1 year ago

Hi @andrejpavlovic, we finally managed to deliver our first push notification. It worked for us after setting the "production" mode in the Salesforce app administration and creating the production build (that we tested on TestFlight). For now, I don't know if there is a possibility to create the "development" mode app (which uses the apple sandbox environment) and send the notifications to the Development build app (that would be helpful to test the setup before building the app).

Thanks for the support. I think that this thread may be helpful for others who will integrate with Salesforce Marketing Cloud.

MarchewkaMatthew commented 1 year ago

It's worth adding that after building the app and publishing it to the TestFlight, Apple will notify you that InfoPlist should contain a description for the NSLocationAlwaysAndWhenInUseUsageDescription

Related documentation from MarketingCloudSdk: https://salesforce-marketingcloud.github.io/MarketingCloudSDK-iOS/location/geolocation-overview.html

It's required even if you set the locationEnabled: false, in the configuration object. Here is the example description for this permission: NSLocationAlwaysAndWhenInUseUsageDescription: "Your location will be used to send push notifications related to your area.",

MarchewkaMatthew commented 1 year ago

Hi @andrejpavlovic,

have you tried building an Android version of the app with this plugin enabled? I'm asking because everything worked for me for the iOS (the device is registered and receives the notifications), but when I tried to test it for Android , I received an error in the eas build (ss in the attached image). Do you know what may be the source of the problem?

image

EDIT: Here is the error path shown in the expo console: at /home/expo/workingdir/build/node_modules/@allboatsrise/expo-marketingcloudsdk/plugin/build/android/index.js:16:71

andrejpavlovic commented 1 year ago

@MarchewkaMatthew I was able to build and test it for Android last week, and it worked pretty well. I'm assuming the issue is that you're using Expo SDK 48, but we're still on SDK 47. You would probably need to update the android plugin where it does search and replace of things, since it looks like it can't find this anchor in the new SDK: https://github.com/allboatsrise/expo-marketingcloudsdk/blob/40b4a4ac68c1c3c1d22fef755c9101ee2a8eb2be/plugin/src/android/index.ts#L20

You would also need to update the SDK to the latest version: https://github.com/allboatsrise/expo-marketingcloudsdk/blob/40b4a4ac68c1c3c1d22fef755c9101ee2a8eb2be/plugin/src/android/index.ts#L32 Change it to 8.0.8.

Not sure if I'll have time to look into upgrading to SDK 48 this week, but probably next week.

andrejpavlovic commented 1 year ago

@MarchewkaMatthew I've released version 48.0.0 of the plugin which works with Expo SDK 48. Mostly I've just tested whether it compiles for iOS and Android, and it seems to be working just fine.

andrejpavlovic commented 1 year ago

Closing this, as it looks like latest version is working well across both platforms.