# Deeplinks on Android SDK Legacy
The handling of deep links in the Legacy SDK will depend on the delivery method selected for the push.
## Messangi API (Legacy)
DeepLinks are handled as additional data included in the payload.
1. Declare BroadcastReceiver
```xml
# Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<application ...>
...
<receiver
android:name=".MessangiReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.ogangi.messangi.android.sdk.PUSH_NOTIFICATION" />
</intent-filter>
</receiver>
...
</application>
</manifest>
```
2. Handler notification
```java
// MessangiReceiver.java
public class MessangiReceiver extends BroadcastReceiver {
private static final String TAG = "MessangiReceiver";
public MessangiReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
// The intent delivers a json representation of the push notification
String json = intent.getStringExtra("message");
MessangiNotification notification;
try {
notification = MessangiNotification.fromJson(json);
TextNotification.show(notification); // Show your notification
} catch (JSONException e) {
Log.e(TAG, "onReceive: ", e);
}
}
}
```
3. Show Notification
```java
// TextNotification.java
public class TextNotification {
private static final String TAG = "notification";
public static void show(final Context context, MessangiNotification notification) {
Log.d(TAG, notification.toJSON());
final NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
final String title = notification.getTitle();
final String text = Html.fromHtml(notification.getText()).toString();
// Handler Deeplink
String deepLink = notification.getExtra("deep-link");
Intent notificationIntent;
switch (deepLink){
case "activity1":
notificationIntent = new Intent(context, MyActivity1.class);
break;
case "activity2":
notificationIntent = new Intent(context, MyActivity2.class);
break
default: // If not match go to MainActivity
notificationIntent = new Intent(context, MainActivity.class);
break;
}
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("NotificationID", notification.getId());
PendingIntent intent = PendingIntent.getActivity(context, notification.getId().hashCode(), notificationIntent, 0);
// Build Notification
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, notification.getPublicKey())
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.ic_stat_image) // Icon of yout app
.setContentTitle(title)
.setContentText(text)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setTicker(text) // Suport text for old devices
.setContentIntent(intent)
.setAutoCancel(true);
// Show Notification
nm.notify(TAG, notification.getId().hashCode(), builder.build());
}
}
```
4. Receive data on Activity (Optional)
Only if you send additional data in the notification and want to retrieve it when opening Activity.
```java
// MyActivity1.java
public class MyActivity1 extends AppCompatActivity {
...
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.getExtras() != null){
String id = intent.getStringExtra("NotificationID");
if(!TextUtils.isEmpty(id)){
List<MessangiNotification> notifications = MessangiNotificationManager.getInstances().getNotifications();
for (MessangiNotification notification: notifications) {
if (notification.getId().equals(id)){
// Here handler notification
break;
}
}
}
}
}
...
}
```
## New Architecture
The new push send architecture works independently of the SDK, so if you want to use this for DeepLink management, just need to follow the Android Official Guides.
### App on Background
1. Declare on Manifest
It's necessary the action property match with the click_action property on notification payload.
```xml
# Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<application ...>
...
<activity android:name=".MyActivity1">
<intent-filter>
<action android:name="activity1"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
...
</application>
</manifest>
```
> When a notification arrives with an click_action property on the payload. If the user clicks on it, Android will be open the Activity with the associated IntentFilter
2. (Optional) Handler on Activity
Only if you send additional data in the notification and want to retrieve it when opening Activity.
```java
// MyActivity1.java
public class MyActivity1 extends AppCompatActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Toolbar myToolbar = findViewById(R.id.toolbar);
setSupportActionBar(myToolbar);
Bundle bundle = getIntent().getExtras();
if ( bundle != null) {
// Handler your data inside the bundle
}
}
...
}
```
### App on Foreground & Silent Push
If the notification don't have visual representation or if the app is in foreground when notification arrive, the notification will be delivered to your FirebaseService.
```java
// FcmListenerService.java
public class FcmListenerService extends FirebaseMessagingService {
private static final String TAG = "FcmListenerService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Handle data payload of FCM messages.
}
@Override
public void onNewToken(String token) {
MessangiLog.debug("FCM Registration Token: " + token);
MessangiInstanceId
.getInstance()
.setPushToken(token);
}
}
```
> If you want to show your notification, even if the app is open, you need to create a custom notification in **onMessageReceived** method.
>
# Deeplink on IOS SDK Legacy
## MessangiAPI (Legacy)
After processing the notification the SDK will posted the notification on pushReceive method
```kotlin
...
func pushReceived(_ message: Message, from workspace:Workspace) {
// This method will be called every time the user receives a push notification
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: message.metaData, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
let deeplink = data['deep-link']
// Handler your deeplink and redirect according.
}
...
```
> In some cases, such as a bad connection, the *pushReceived* method may not be called, so we always recommend checking and handling the deepLink data in **didReceiveRemoteNotification** and **willPresent notification**.
## New Architecture
### App on Background
```kotlin
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if(userInfo['deep-link']){
// Handler your deeplink and redirect according.
}
Messangi.sharedInstance().processRemoteNotification(userInfo, fetchCompletionHandler: completionHandler)
}
```
### App on Foreground
```kotlin
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
//Invoked when a notification is sent to a foreground application.
let userInfo = notification.request.content.userInfo
let completion: ((UIBackgroundFetchResult) -> Void)? = { internalHandlerResponse in
if(userInfo['deep-link']){
// Handler your deeplink and redirect according.
}
completionHandler([.alert, .sound])
}
Messangi.sharedInstance().processRemoteNotification(userInfo as? [AnyHashable : Any], fetchCompletionHandler: completion)
}
```