Android SDK Setting

From AdLocus
Jump to: navigation, search

Getting Start with AdLocus

環境設置

  • Min SDK : 19
  • Compile Sdk Version : 26

Dependency lib

Hyxen SDK

//implementation(name: 'adlocusaar-debug', ext: 'aar')
implementation 'com.hyxen.adlocusaar:adlocusaar:4.1.09'

系統

implementation 'com.android.support:appcompat-v7:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

Json parser

// Json parser
annotationProcessor 'com.bluelinelabs:logansquare-compiler:1.3.6'
implementation 'com.bluelinelabs:logansquare:1.3.7'
implementation "com.github.aurae.retrofit2:converter-logansquare:1.4.1"

Http agent

// Http agent
implementation 'com.squareup.retrofit2:retrofit:2.2.0'

RxJava

implementation 'io.reactivex.rxjava2:rxjava:2.0.6'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'

Google

// 如果有用到 Google Ads 請使用以下版本
implementation 'com.google.android.gms:play-services-ads:15.0.1'

Init SDK設置

  • App First Activity
  • 在string.xml 放置 FCM APP key
  • 在AD Locus後台的App key
  • 在Application嘗試取得FCM token,init SDK:
//Init new AdLocus SDK
FirebaseApp.initializeApp(this);
String token = FirebaseInstanceId.getInstance().getToken();
AdLocus.getInstance(this).checkUserStatement
({your fcm token}, {your fcm app key},{your package name}, {your hynex app key});
  • Example:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
       && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
   String token = FirebaseInstanceId.getInstance().getToken();
   AdLocus.getInstance(this)
           .checkUserStatement(!TextUtils.isEmpty(token) ? token : "",
                   getString(R.string.fcm_app_key), getPackageName(), getString(R.string.app_key));
} else {
   ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, TAG_LOCATION);
}
  • Permission result(不管是否允許permission,都需要init new SDK)
@Override
public void onRequestPermissionsResult(int requestCode,
                                      String permissions[], int[] grantResults) {
   switch (requestCode) {
       case TAG_LOCATION: {
           // If request is cancelled, the result arrays are empty.
           if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
               // permission was granted, yay! Do the
               // contacts-related task you need to do.
               String token = FirebaseInstanceId.getInstance().getToken();
               AdLocus.getInstance(this)
                       .checkUserStatement(!TextUtils.isEmpty(token) ? token : "",
                               getString(R.string.fcm_app_key), getPackageName(), getString(R.string.app_key));
           } else {
               String token = FirebaseInstanceId.getInstance().getToken();
               AdLocus.getInstance(this)
                       .checkUserStatement(!TextUtils.isEmpty(token) ? token : "",
                               getString(R.string.fcm_app_key), getPackageName(), getString(R.string.app_key));
           }
           return;
       }
   }
}
  • 建立FCM Token receiver:
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
   private final static String TAG = MyFirebaseInstanceIDService.class.getSimpleName();
 
   @Override
   public void onTokenRefresh() {
       // Get updated InstanceID token.
       String refreshedToken = FirebaseInstanceId.getInstance().getToken();
       Logger.d(TAG, "Refreshed token: " + refreshedToken);
 
       // If you want to send messages to this application instance or
       // manage this apps subscriptions on the server side, send the
       // Instance ID token to your app server.
       sendRegistrationToServer(refreshedToken);
   }
   // [END refresh_token]
 
   /**
    * Persist token to third-party servers.
    * <p>
    * Modify this method to associate the user's FCM InstanceID token with any server-side account
    * maintained by your application.
    *
    * @param token The new token.
    */
   private void sendRegistrationToServer(String token) {
       AdLocus.getInstance().updatePushToken(token);
   }
}
  • 建立FCM Message receiver
  • 將收到GetFCMDataResponse.TAG_TARGET的Key若mapping到Constants.TAG_FCM_TARGET則callAdLocus.getInstance().sendFCMMessage(this,data)將FCM傳入SDK
public class MyFirebaseMessagingService extends FirebaseMessagingService {
 
   private static final String TAG = "MyFirebaseMsgService";
 
   /**
    * Called when message is received.
    *
    * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
    */
   // [START receive_message]
   @Override
   public void onMessageReceived(RemoteMessage remoteMessage) {
       Logger.d(TAG, "From: " + remoteMessage.getFrom());
 
       // Check if message contains a data payload.
       if (remoteMessage.getData().size() > 0) {
           Map<String, String> data = remoteMessage.getData();
           Logger.d(TAG, "Message data payload: " + remoteMessage.getData());
           if (TextUtils.equals(data.get(GetFCMDataResponse.TAG_TARGET), Constants.TAG_FCM_TARGET))
               AdLocus.getInstance().sendFCMMessage(this,data);
           if (/* Check if data needs to be processed by long running job */ true) {
               // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
//                scheduleJob();
           } else {
               // Handle message within 10 seconds
//                handleNow();
           }
       }
   }
}
  • 在manifests將FCM message reveiver與FCM token receiver import:
<!-- FCM -->
<service android:name=".MyFirebaseInstanceIDService">
   <intent-filter>
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
   </intent-filter>
</service>
 
<service android:name=".MyFirebaseMessagingService">
   <intent-filter>
       <action android:name="com.google.firebase.MESSAGING_EVENT" />
   </intent-filter>
</service>

備註

  • 如有使用混淆程式碼,必須在 proguard-android.txt 加入
 keep class com.hyxen.adlocusaar.** {*;}
 keep interface com.hyxen.adlocusaar.** {*;}
 keep enum com.hyxen.adlocusaar.** {*;}
 dontwarn com.squareup.okhttp3.**
 keep class com.squareup.okhttp3.** { *;}
 dontwarn okio.**