Skip to content

Commit

Permalink
Merge pull request #4 from grupo-sbf/main
Browse files Browse the repository at this point in the history
SDK version updated
  • Loading branch information
alextarrago authored Aug 3, 2023
2 parents 032dc0d + e1adf4f commit f7e02e5
Show file tree
Hide file tree
Showing 15 changed files with 342 additions and 287 deletions.
11 changes: 7 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
## 1.1.0

- SDK version updated

## 1.0.2

* Clean code and Fix conventions
- Clean code and Fix conventions

## 1.0.1

* Clean code and Fix conventions
- Clean code and Fix conventions

## 1.0.0

* First release of SFMC plugin for Fluter

- First release of SFMC plugin for Fluter
259 changes: 127 additions & 132 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,149 +22,144 @@ dependencies:
sfmc_flutter: <latest_version>
```
In your library add the following import:
```dart
import 'package:sfmc_flutter/sfmc_flutter.dart';
```

## Getting started
### Setup Android
1. Add SFMCSdk to project-level `build.gradle`
```gradle
allprojects {
repositories {
...
maven {
url "https://salesforce-marketingcloud.github.io/MarketingCloudSDK-Android/repository"
}
}
}
```
2. Add SFMCSdk to app-level `app/build.gradle`
```gradle
implementation ("com.salesforce.marketingcloud:marketingcloudsdk:8.0.4")
implementation 'com.google.android.gms:play-services-location:17.1.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation platform('com.google.firebase:firebase-bom:29.0.0')
implementation 'com.google.firebase:firebase-messaging:20.1.2'
```
3. Add your `google-services.json` from your Firebase to `app/`.
4. Open `app/src/main/<your_package_name>/MainActivity.kt` and add the following code.

```kotlin
...
import io.flutter.embedding.android.FlutterActivity
import com.salesforce.marketingcloud.MarketingCloudSdk
import com.salesforce.marketingcloud.MCLogListener
import com.salesforce.marketingcloud.MarketingCloudConfig
import com.salesforce.marketingcloud.notifications.NotificationCustomizationOptions
import com.salesforce.marketingcloud.sfmcsdk.SFMCSdk
import com.salesforce.marketingcloud.sfmcsdk.SFMCSdkModuleConfig


class MainActivity : FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
SFMCSdk.configure(applicationContext as Application, SFMCSdkModuleConfig.build {
pushModuleConfig = MarketingCloudConfig.builder().apply {
setApplicationId("<your_application_id>")
setAccessToken("<your_access_token>")
setSenderId("<your_sender_id>")
setMarketingCloudServerUrl("<your_marketing_cloud_url>")
setMid("<your_mid>")
setNotificationCustomizationOptions(
NotificationCustomizationOptions.create(R.drawable.ic_notification_icon)
)
}.build(applicationContext)
}) { initStatus ->
}
}
}
1. [Connect Marketing Cloud Account to Your Android App](https://salesforce-marketingcloud.github.io/MarketingCloudSDK-Android/create-apps/create-apps-overview.html)
2. Folow [Firebase guide](https://firebase.google.com/docs/android/setup#console) (just until step 3.1) to add your `google-services.json` to `app/` directory.
3. (Optional) By default the icon used in notification is the app icon, but you can define other in `android/app/src/main/AndroidManifest.xml`:

```xml
<meta-data
android:name="SFCMNotificationIcon"
android:resource="@drawable/custom_icon_here" />
```

### Setup iOS
1. Setup your `AppDelegate.swift` file with Marketing Cloud initialization.

1. [Create a .p8 Auth Key File](https://salesforce-marketingcloud.github.io/MarketingCloudSDK-iOS/get-started/get-started-provision.html)
2. [Setup MobilePush Apps](https://salesforce-marketingcloud.github.io/MarketingCloudSDK-iOS/get-started/get-started-setupapps.html)
3. Enable push notifications in your target’s [Capabilities settings](https://salesforce-marketingcloud.github.io/MarketingCloudSDK-iOS/assets/SDKConfigure6.png).
4. Setup your `AppDelegate.swift` file to handle push notification.

```swift
import UIKit
import Flutter
import MarketingCloudSDK

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
self.configureMarketingCloudSDK()
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}

func configureMarketingCloudSDK() {
let builder = MarketingCloudSDKConfigBuilder()
.sfmc_setApplicationId("<your_application_id>")
.sfmc_setAccessToken("<your_access_token>")
.sfmc_setMarketingCloudServerUrl("<your_marketing_cloud_url>")
.sfmc_setMid("<your_mid>")
.sfmc_build()!

do {
try MarketingCloudSDK.sharedInstance().sfmc_configure(with:builder)
registerForRemoteNotification()
} catch let error as NSError {

}
}

override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
MarketingCloudSDK.sharedInstance().sfmc_setDeviceToken(deviceToken)
}
func registerForRemoteNotification() {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()

center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
UIApplication.shared.registerForRemoteNotifications()
}
}

}
else {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
}

override func applicationProtectedDataDidBecomeAvailable(_ application: UIApplication) {
if(MarketingCloudSDK.sharedInstance().sfmc_isReady() == false) {
self.configureMarketingCloudSDK()
}
}
import UIKit
import Flutter
import MarketingCloudSDK
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
registerForRemoteNotification()
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
// MobilePush SDK: REQUIRED IMPLEMENTATION
override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
MarketingCloudSDK.sharedInstance().sfmc_setDeviceToken(deviceToken)
}
// MobilePush SDK: REQUIRED IMPLEMENTATION
override func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print(error)
}
// MobilePush SDK: REQUIRED IMPLEMENTATION
/** This delegate method offers an opportunity for applications with the "remote-notification" background mode to fetch appropriate new data in response to an incoming remote notification. You should call the fetchCompletionHandler as soon as you're finished performing that operation, so the system can accurately estimate its power and data cost.
This method will be invoked even if the application was launched or resumed because of the remote notification. The respective delegate methods will be invoked first. Note that this behavior is in contrast to application:didReceiveRemoteNotification:, which is not called in those cases, and which will not be invoked if this method is implemented. **/
override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
MarketingCloudSDK.sharedInstance().sfmc_setNotificationUserInfo(userInfo)
completionHandler(.newData)
}
// MobilePush SDK: REQUIRED IMPLEMENTATION
// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from applicationDidFinishLaunching:.
@available(iOS 10.0, *)
override func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// Required: tell the MarketingCloudSDK about the notification. This will collect MobilePush analytics
// and process the notification on behalf of your application.
MarketingCloudSDK.sharedInstance().sfmc_setNotificationRequest(response.notification.request)
completionHandler()
}
// MobilePush SDK: REQUIRED IMPLEMENTATION
// The method will be called on the delegate only if the application is in the foreground. If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented. The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list. This decision should be based on whether the information in the notification is otherwise visible to the user.
@available(iOS 10.0, *)
override func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert)
}
private func registerForRemoteNotification() {
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: { _, _ in }
)
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
}
UIApplication.shared.registerForRemoteNotifications()
}
}
```

### Flutter
### Setup Flutter

```dart
await SFMCSDK.setContactKey("<contact_id>"); // Set Contact Key for desired user
await SFMCSDK.enablePush(); // Enables PUSH for SDK
await SFMCSDK.disablePush(); // Disables PUSH for SDK
await SFMCSDK.pushEnabled(); // Returns if push is enabled or not
await SFMCSDK.setAttribute("name", "Mark"); // Set a user attribute
await SFMCSDK.clearAttribute("name"); // Removes a given user attribute
await SFMCSDK.setTag("Barcelona"); // Set a user tag
await SFMCSDK.removeTag("Barcelona"); // Removes a given user tag
await SFMCSDK.enableVerbose(); // Enable native Verbose
await SFMCSDK.disableVerbose(); // Disable native Verbose
await SFMCSDK.sdkState(); // Returns the SDKState log
import 'package:flutter/foundation.dart';
import 'package:sfmc_flutter/sfmc_flutter.dart';
// and any others imports...
void main() {
setupSFMC();
runApp(const MyApp());
}
Future<void> setupSFMC() async {
if (kDebugMode) {
await SFMCSDK.enableVerbose();
}
await SFMCSDK.setupSFMC(
appId: "{mc_application_id}",
accessToken: "{mc_access_token}",
mid: "{mid}",
sfmcURL: "{marketing_cloud_url}",
senderId: "{fcm_sender_id}",
delayRegistration: true,
);
}
```

#### Other available methods
```dart
await SFMCSDK.setContactKey("<contact_id>"); // Set Contact Key for desired user
await SFMCSDK.enablePush(); // Enables PUSH for SDK
await SFMCSDK.disablePush(); // Disables PUSH for SDK
await SFMCSDK.pushEnabled(); // Returns if push is enabled or not
await SFMCSDK.setAttribute("name", "Mark"); // Set a user attribute
await SFMCSDK.clearAttribute("name"); // Removes a given user attribute
await SFMCSDK.setTag("Barcelona"); // Set a user tag
await SFMCSDK.removeTag("Barcelona"); // Removes a given user tag
await SFMCSDK.enableVerbose(); // Enable native Verbose
await SFMCSDK.disableVerbose(); // Disable native Verbose
await SFMCSDK.sdkState(); // Returns the SDKState log
```

## Contributions
Expand Down
15 changes: 9 additions & 6 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ android {
defaultConfig {
minSdkVersion 16
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "com.salesforce.marketingcloud:marketingcloudsdk:8.0.4"
implementation 'com.google.android.gms:play-services-location:17.1.0'
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "com.salesforce.marketingcloud:marketingcloudsdk:8.0.9"
implementation 'com.google.android.gms:play-services-location:17.1.0'

implementation platform("com.google.firebase:firebase-bom:30.3.2")
implementation 'com.google.firebase:firebase-messaging'
}
}
Loading

0 comments on commit f7e02e5

Please sign in to comment.