‹ Misc Doc Home

Misc Integration Guide

For the C++ version of cocos2d-x v3.x - (all other versions)

Integration

Open a terminal and use the following command to install the SDKBOX Misc plugin.

$ sdkbox import misc

Extern iOS Modification

File change list:


  #import "RootViewController.h"

+ #include "PluginMisc/PluginMisc.h"

  @implementation AppController


  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      ...
      //run the cocos2d-x game scene
      app->run();

+     if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
+         [self handleLocalNotification:launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]];
+     }

      return YES;
  }

+ - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
+     [self handleLocalNotification:notification.userInfo];
+ }
+ 
+ - (void)handleLocalNotification:(NSDictionary *)payloadDic {
+     NSError *error = nil;
+     NSData *jsonData = [NSJSONSerialization dataWithJSONObject:payloadDic
+                                                        options:0
+                                                          error:&error];
+     if (nil != jsonData) {
+         NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
+         sdkbox::PluginMisc::handleLocalNotify([jsonString UTF8String]);
+     } else {
+         NSLog(@"Error:%@", error);
+     }
+ }

  - (void)applicationWillResignActive:(UIApplication *)application {

Extern Android Modification

File change list:

  package org.cocos2dx.cpp;

+ import android.content.Intent;
  import android.os.Bundle;
  import org.cocos2dx.lib.Cocos2dxActivity;

- public class AppActivity extends Cocos2dxActivity {
+ public class AppActivity extends com.sdkbox.plugin.SDKBoxActivity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
              return;
          }
          // DO OTHER INITIALIZATION BELOW

+         com.sdkbox.plugin.PluginMisc.onHandleNotification(getIntent());

      }

+     @Override
+     protected void onNewIntent(Intent intent) {
+         super.onNewIntent(intent);
+         com.sdkbox.plugin.PluginMisc.onHandleNotification(intent);
+     }

  }

-proj.android/app/AndroidManifest.xml

add `singleTask` to your activity
              android:screenOrientation="landscape"
              android:configChanges="orientation|keyboardHidden|screenSize"
              android:label="@string/app_name"
              android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
+             android:launchMode="singleTask" >
              <intent-filter>
                  <action android:name="android.intent.action.MAIN" />

JSON Configuration

SDKBOX Installer will automatically inject a sample configuration to your sdkbox_config.json

Example:


{
    "ios": {
        "Misc":{
        }
    },
    "android": {
        "Misc":{
        }
    }
}

Usage

Initialize Misc

Initialize the plugin where appropriate in your code. We recommend to do this in the AppDelegate::applicationDidFinishLaunching() or AppController:didFinishLaunchingWithOptions(). Make sure to include the appropriate headers. Example:

+ #include "PluginMisc/PluginMisc.h"
  AppDelegate::applicationDidFinishLaunching() {
+      sdkbox::PluginMisc::init();
  }

send local notify

you will receive local notify in listener

int nid = sdkbox::PluginMisc::localNotify("test title", "this a test notify content", 1000 * 10);
std::stringstream buf;
buf << "Local Notification:" << nid;
CCLOG(buf.str());

Implement MiscListner

#include "PluginMisc/PluginMisc.h"
class MyClass : public sdkbox::MiscListener
{
private:
    virtual void onHandleLocalNotify(const std::string& payloadJson) {
        // recevie local notification
        std::stringstream buf;
        buf << "onHandleLocalNotify:" << payloadJson;
        cocos2d::log("Log: %s", buf.str().c_str());
    };
}

API Reference

Methods

static bool init ( ) ;

initialize the plugin instance.

static void setListener ( MiscListener * listener ) ;

Set listener to listen for misc events

static MiscListener * getListener ( ) ;

Get the listener

static void removeListener ( ) ;

Remove the listener, and can't listen to events anymore

static int localNotify ( const std::string & title ,
                         const std::string & content ,
                         int delayMillisecond ) ;

Local Notification

title: notification title, just valid on android
content: notification content
delayMillisecond: delay millisecond to notify, just valid on iOS

return notifyID
static void cleanLocalNotify ( int notifyID = 0 ) ;

Clear Local Notification

notificationID: notification id, 0: will cancel all local notify
static void handleLocalNotify ( const std::string & notificationUserInfo ) ;

Handle Local Notification, just For iOS

on Android, please use com.sdkbox.plugin.PluginMisc.onHandleNotification(intent);

static std::string getPlatformName ( ) ;

get current platform, iOS or Android

static std::string getMetaData ( const std::string & name ) ;

just valid on android, can get meta data from AndroidManifest.xml

static std::string getIAPProvider ( ) ;

get current iap provider of SDKBox IAP return 'Apple', 'Google', 'Amazon' or 'Playphone'

static std::string getAppVersion ( ) ;

get app version

static std::string getAppBuildVersion ( ) ;

get iOS app build version

static int getAppVersionCode ( ) ;

get android app version code

static std::string getDeviceInfo ( ) ;

get device info

static void setKeychainService ( const std::string & service ) ;

set keychain service

static void setKeychainAccessGroup ( const std::string & group ) ;

set keychain accessgroup

static int storeStringInKeychain ( const std::string & account ,
                                   const std::string & value ) ;

store account data in keychain, if exist will update it return SecurityFrameworkResultCode: https://developer.apple.com/documentation/security/1542001-security_framework_result_codes

static const std::string fetchStringInKeychain ( const std::string & account ) ;

fetch account data from keychain

static int removeDataInKeychain ( const std::string & account ) ;

remove account in keychain

Listeners

void onHandleLocalNotify ( const std::string & payloadJson ) 

Notifies the delegate that user tap the notify

Manual Integration

If the SDKBOX Installer fails to complete successfully, it is possible to integrate SDKBOX manually. If the installer complete successfully, please do not complete anymore of this document. It is not necessary.

These steps are listed last in this document on purpose as they are seldom needed. If you find yourself using these steps, please, after completing, double back and re-read the steps above for other integration items.

Manual Integration For iOS

Drag and drop the following frameworks from the plugins/ios folder of the Misc bundle into your Xcode project, check Copy items if needed when adding frameworks:

sdkbox.framework

PluginMisc.framework

You also need to add the following system frameworks, if you don't already have them:

SystemConfiguration.framework

file change list:


  #import "RootViewController.h"

+ #include "PluginMisc/PluginMisc.h"

  @implementation AppController


  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      ...
      //run the cocos2d-x game scene
      app->run();

+     if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
+         [self handleLocalNotification:launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]];
+     }

      return YES;
  }

+ - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
+     [self handleLocalNotification:notification.userInfo];
+ }
+ 
+ - (void)handleLocalNotification:(NSDictionary *)payloadDic {
+     NSError *error = nil;
+     NSData *jsonData = [NSJSONSerialization dataWithJSONObject:payloadDic
+                                                        options:0
+                                                          error:&error];
+     if (nil != jsonData) {
+         NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
+         sdkbox::PluginMisc::handleLocalNotify([jsonString UTF8String]);
+     } else {
+         NSLog(@"Error:%@", error);
+     }
+ }

  - (void)applicationWillResignActive:(UIApplication *)application {

Manual Integration For Android Studio

  package org.cocos2dx.cpp;

+ import android.content.Intent;
  import android.os.Bundle;
  import org.cocos2dx.lib.Cocos2dxActivity;

- public class AppActivity extends Cocos2dxActivity {
+ public class AppActivity extends com.sdkbox.plugin.SDKBoxActivity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
              return;
          }
          // DO OTHER INITIALIZATION BELOW

+         com.sdkbox.plugin.PluginMisc.onHandleNotification(getIntent());

      }

+     @Override
+     protected void onNewIntent(Intent intent) {
+         super.onNewIntent(intent);
+         com.sdkbox.plugin.PluginMisc.onHandleNotification(intent);
+     }

  }


  dependencies {
      implementation fileTree(dir: 'libs', include: ['*.jar'])
      implementation project(':libcocos2dx')
+     implementation 'com.android.support:support-v4:26.1.0'
  }

-proj.android/app/AndroidManifest.xml add singleTask to your activity

              android:screenOrientation="landscape"
              android:configChanges="orientation|keyboardHidden|screenSize"
              android:label="@string/app_name"
              android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
+             android:launchMode="singleTask" >
              <intent-filter>
                  <action android:name="android.intent.action.MAIN" />

                   $(LOCAL_PATH)/../../../Classes/AppDelegate.cpp \
                   $(LOCAL_PATH)/../../../Classes/HelloWorldScene.cpp

+ LOCAL_CPPFLAGS := -DSDKBOX_ENABLED
  LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../Classes
+ LOCAL_WHOLE_STATIC_LIBRARIES := PluginMisc sdkbox

  # _COCOS_HEADER_ANDROID_BEGIN
  # _COCOS_HEADER_ANDROID_END
  include $(BUILD_SHARED_LIBRARY)

  $(call import-add-path, $(LOCAL_PATH)/../../../cocos2d)
+ $(call import-add-path, $(LOCAL_PATH))
  $(call import-module, cocos)
+ $(call import-module, ./sdkbox)
+ $(call import-module, ./PluginMisc)

  # _COCOS_LIB_IMPORT_ANDROID_BEGIN

Proguard (optional)

proguard.config=proguard.cfg
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

# cocos2d-x
-keep public class org.cocos2dx.** { *; }
-dontwarn org.cocos2dx.**
-keep public class com.chukong.** { *; }
-dontwarn com.chukong.**

#sdkbox
-keep public class com.sdkbox.** { *; }
-dontwarn com.sdkbox.**

Note: Proguard only works with Release builds (i.e cocos run -m release) debug builds do not invoke Proguard rules.