Push Notifications

Learn how to make push notifications work in your RN app

To make promo campaigns work you need to set up remote notifications handling for every platform you are using. iOS is somewhat simple, but Android

iOS

You have to pass push notifications to Adapty SDK to check, whether notifications are promo-based. To do this you have to tweak AppDelegate.m for iOS notifications.

You'll need to have Adapty pod installed. (pod install with react-native-adapty will do this)

🚧

Working with a Swift libraries

Make sure, that you have Swift Bridging Header in your project set up

In AppDelegate you need to import Adapty @import Adapty; and #import <Adapty/Adapty-umbrella.h>. Then inside AppDelegate implementation you need to add a didReceiveRemoteNotification section.

// imports ...
@import Adapty;
#import <Adapty/Adapty-umbrella.h>

// and then inside AppDelegate:
// @implementation AppDelegate
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
  [Adapty handlePushNotification:userInfo completion:^(AdaptyError *_) {
   completionHandler(UIBackgroundFetchResultNewData);
  }];
}
//...
// @end

Afterwards, Adapty push notifications should work for iOS

Android

We use Firebase Cloud Messaging (FCM) to send push notifications from Adapty. Here you will find instructions how to set up FCM in your RN app with Adapty support

0. Set up Firebase project and create configuration file

0.1 If you haven't already, you need to create a Firebase project. Then create your unique google-services.json file and apply it to your project. You can use official tutorial until Edit your app manifest if you aren't sure how to do this

0.2 If you haven't already, you need to pass a FCM server key to Adapty Dashboard. This tutorial can guide you through

1. Add dependencies

You will need Kotlin and FCM set up in your project:

1.1 Go to android/build.gradle. Make sure these dependencies (or newer versions) exist:

buildscript {
  repositories {
        // ...
    google() // <- this is where you can download FCM from
  }
  dependencies {
    // ...
    classpath 'com.google.gms:google-services:4.3.10' // <- this is FCM
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10" // <- this is Kotlin
}

1.2 Go to android/app/build.gradle. Make sure these dependencies (or newer versions) exist:

dependencies {
    implementation 'com.google.firebase:firebase-messaging:23.0.0' // <- this is for FCM
    implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.10" // <- this is for Kotlin
}

1.3 Sync your project with Gradle files

2. Create a push handler

2.1 (Optional) Create Android notification icons. Official guidelines

2.2 In your android project directory android/app/src/main/java/{PROJECT_ID} create a file YourAdaptyPushHandler.java. Fill with such content:

package {PROJECT_ID};

import {PROJECT_ID}.R; // <- this is the assets you've created in step 2.1

import android.content.Context;
import androidx.annotation.NonNull;
import com.adapty.push.AdaptyPushHandler;


public class YourAdaptyPushHandler extends AdaptyPushHandler {
    public YourAdaptyPushHandler(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public String getClickAction() {
        return "YOUR_NOTIFICATION_CLICK_ACTION";
    }

    @Override
    public int getSmallIconResId() {
        return R.drawable.ic_notification_small; // <- this is an example of an icon. Replace with yours 
    }
}

{PROJECT_ID} being a string like com.adapty.example

3. Create a FM service

3.1 In your android project directory android/app/src/main/java/{PROJECT_ID} create a file YourFirebaseMessagingService.java. Fill with such content:

package {PROJECT_ID};

/**
 * Setup Firebase Cloud Messaging (https://firebase.google.com/docs/cloud-messaging/android/client),
 * add your google-services.json and uncomment this section
 */

import androidx.annotation.NonNull;

import com.adapty.Adapty;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

class YourFirebaseMessagingService extends FirebaseMessagingService {
    private final YourAdaptyPushHandler pushHandler = new YourAdaptyPushHandler(this);

    @Override
    public void onNewToken(@NonNull String s) {
        super.onNewToken(s);
        Adapty.refreshPushToken(s);
    }

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        if (!pushHandler.handleNotification(remoteMessage.getData())) {
            /*
            here is your logic for other notifications
            that haven't been handled by Adapty
             */
        }
    }
}

3.2 Declare your service inside manifest.application in android/app/src/main/AndroidManifest.xml:

# <manifest>
#   <uses-permission ... />
#   ...
#   <application ...>
#     <activity ...>
#        ...
#      </activity>
#      ...

       <service android:name=".YourFirebaseMessagingService" android:exported="false">
         <intent-filter>
           <action android:name="com.google.firebase.MESSAGING_EVENT" />
         </intent-filter>
       </service>
  
#   </application>
# </manifest>

4. Set up click handling

4.1 Inside ‘android/app/src/main/java/{PROJECT_ID}/MainActivity.java’ in a ‘MainActivity’ class implement these methods:

// imports ...

import android.content.Intent;
import android.os.Bundle;
import com.adapty.Adapty;

public class MainActivity extends ReactActivity {
  // ...
  
  //add this method
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    handleIntent(getIntent());
  }

  // add this method
  @Override
  public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    handleIntent(intent);
  }

  // add this method
  private void handleIntent(Intent intent) {
    if (Adapty.handlePromoIntent(intent, (promoModel, adaptyError) -> {
      //your logic for callback
      return null;
    })) {
      //your logic for the case user did click on promo notification,
      //for example show loading indicator
    } else {
      //your logic for other cases
     }
   }
}

Afterwards, Adapty push notifications should work for Android devices


Did this page help you?