Usecase: Launch via notification

Let's see how we can launch a project via notification

Step 1: Generate project_id

To generate anproject_idin the Leap dashboard, refer to this. Also, you can refer below:
Make sure to send the correct project_id with correct app version in the notification.

Step 2: Create a Cloud Notification

For this example, we will be showing notification via Firebase. Let's see how to create a notification in Firebase. We won't publish the notification yet, as in the next step we will add the code on the Android side to launch the notification.

2.a.Compose a notification

2.b.Add leap project_id

Don't publish the notification yet, we need to add code in the Android project to launch the Leap project via notification.

Step 3: Launching leap via notification

To launch the leap project via notification you will have to process the project_id passed in the firebase dashboard (in your Android project).

3.a.Process the firebase data in your Firebase service class:

Android
Swift
public class LeapFirebaseService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
RemoteMessage.Notification remoteNotification = remoteMessage.getNotification();
if (remoteNotification != null) {
//1. Receive the data from Firebase
Map<String, String> data = remoteMessage.getData();
String projectId = data.get("project_id");
//2. Create an intent (with project_id data as extra) and specify the destination
Intent intent = new Intent(this, DestinationActivity.class);
intent.putExtra("project_id", projectId);
//3. Since we are launching a notification, we need to create a PendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(this,
REQUEST_CODE,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
//4. Create a notification with the above created PendingIntent
Notification notification = getNotification(this,
remoteNotification.getTitle(),
remoteNotification.getBody(),
"Launch",
pendingIntent); // "Launch" is the action button text
//5. Let's launch the notification
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
}
}
}
import UIKit
import Firebase
import FirebaseMessaging
import UserNotifications
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
Messaging.messaging().delegate = self
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: { _, _ in }
)
application.registerForRemoteNotifications()
return true
}

3.b.Launch leap project

Launch the project in destination screen using Leap.embedProject(String project_id)
Android
Swift
class DestinationActivity extends Activity {
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//1. Since the intent destination was here, we will handle the received intent here
if (intent != null && intent.hasExtra("project_id")) {
//2. Get the project_id from intent
String projectId = intent.getStringExtra("project_id");
//3. Verify the project_id is not null or not empty
if (projectId != null && !projectId.isEmpty()) {
//4. Launch the project_id via Leap SDK's 'embedProject' method
Leap.embedProject(projectId);
}
}
}
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
if let projectId = userInfo["projectId"] as? String {
Leap.shared.embedProject(projectId)
}
}

Step 4: Run on device

4.a.Publish the notification

4.b. Check the app

After publishing the notification from the Firebase dashboard, it should be visible on your app!
Android
iOS