Misc Integration Guide
For the Javascript 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:
proj.ios_mac/ios/AppController.mm
#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:
-
proj.android/app/src/org/cocos2dx/cpp/AppActivity.java
this should be your app activity java file
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 by calling init()
where appropriate in your code. We
recommend to do this in the app.js
. Example:
sdkbox.PluginMisc.init();
send local notification
// will receive notification after 10 second
let nid = sdkbox.PluginMisc.localNotify("test title", "this a test notify content", 1000 * 10);
cc.log('Local Notification ID:' + nid);
Implement MiscListner
- You can implement MiscListener if you want to receive local notification event.
sdkbox.PluginMisc.setListener({
onHandleLocalNotify :function (json) {
cc.log(json);
}
});
API Reference
Methods
sdkbox.PluginMisc.init();
initialize the plugin instance.
sdkbox.PluginMisc.setListener(listener);
Set listener to listen for misc events
sdkbox.PluginMisc.localNotify(title, content, delayMillisecond);
Local Notification
title: notification title, just valid on android content: notification content delayMillisecond: delay millisecond to notify, just valid on iOS return notifyID
sdkbox.PluginMisc.cleanLocalNotify(notifyID);
Clear Local Notification
notificationID: notification id, 0: will cancel all local notify
sdkbox.PluginMisc.handleLocalNotify(notificationUserInfo);
Handle Local Notification, just For iOS
on Android, please use com.sdkbox.plugin.PluginMisc.onHandleNotification(intent);
sdkbox.PluginMisc.getPlatformName();
get current platform, iOS or Android
sdkbox.PluginMisc.getMetaData(name);
just valid on android, can get meta data from AndroidManifest.xml
sdkbox.PluginMisc.getIAPProvider();
get current iap provider of SDKBox IAP return 'Apple', 'Google', 'Amazon' or 'Playphone'
sdkbox.PluginMisc.getAppVersion();
get app version
sdkbox.PluginMisc.getAppBuildVersion();
get iOS app build version
sdkbox.PluginMisc.getAppVersionCode();
get android app version code
sdkbox.PluginMisc.getDeviceInfo();
get device info
sdkbox.PluginMisc.setKeychainService(service);
set keychain service
sdkbox.PluginMisc.setKeychainAccessGroup(group);
set keychain accessgroup
sdkbox.PluginMisc.storeStringInKeychain(account, value);
store account data in keychain, if exist will update it return SecurityFrameworkResultCode: https://developer.apple.com/documentation/security/1542001-security_framework_result_codes
sdkbox.PluginMisc.fetchStringInKeychain(account);
fetch account data from keychain
sdkbox.PluginMisc.removeDataInKeychain(account);
remove account in keychain
Listeners
onHandleLocalNotify(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:
- proj.ios_mac/ios/AppController.mm
#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 {
Copy all source and header files from plugin/jsbindings/
to your projects Classes
folder.
NOTE: plugin/jsbindings/jsb2
for creator 1.7.
Add these same files, that you just copied, to Xcode by either dragging and dropping them into Xcode or by using File -> Add files to....
Manual Integration For Android Studio
- proj.android/app/libs <- $PLUGIN_MISC_BUNDLE/plugin/android/libs
- proj.android/app/jni <- $PLUGIN_MISC_BUNDLE/plugin/android/jni
- proj.android/app/src/org/cocos2dx/cpp/AppActivity.java
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/build.gradle
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" />
- frameworks/runtime-src/Classes/.cpp <- $PLUGIN_MISC_BUNDLE/plugin/jsbindings/.cpp
- frameworks/runtime-src/Classes/.h <- $PLUGIN_MISC_BUNDLE/plugin/jsbindings/.h
- frameworks/runtime-src/Classes/.hpp <- $PLUGIN_MISC_BUNDLE/plugin/jsbindings/.hpp
if you use creator 1.7+, you should copy js binding file in jsb2:
- frameworks/runtime-src/Classes/.cpp <- $PLUGIN_MISC_BUNDLE/plugin/jsbindings/jsb2/.cpp
- frameworks/runtime-src/Classes/.h <- $PLUGIN_MISC_BUNDLE/plugin/jsbindings/jsb2/.h
-
frameworks/runtime-src/Classes/.hpp <- $PLUGIN_MISC_BUNDLE/plugin/jsbindings/jsb2/.hpp
-
proj.android/app/jni/Android.mk
$(LOCAL_PATH)/../../../Classes/AppDelegate.cpp \
- $(LOCAL_PATH)/../../../Classes/HelloWorldScene.cpp
+ $(LOCAL_PATH)/../../../Classes/HelloWorldScene.cpp \
+ $(LOCAL_PATH)/../../../Classes/SDKBoxJSHelper.cpp \
+ $(LOCAL_PATH)/../../../Classes/PluginMiscJS.cpp \
+ $(LOCAL_PATH)/../../../Classes/PluginMiscJSHelper.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
./frameworks/runtime-src/Classes/AppDelegate.cpp
register javascript functions
#endif
USING_NS_CC;
+ #ifdef SDKBOX_ENABLED
+ #include "PluginMiscJS.hpp"
+ #include "PluginMiscJSHelper.h"
+ #endif
AppDelegate::AppDelegate(int width, int height) : Application("Cocos Game", width, height)
...
jsb_register_all_modules();
+ #ifdef SDKBOX_ENABLED
+ se->addRegisterCallback(register_all_PluginMiscJS);
+ se->addRegisterCallback(register_all_PluginMiscJS_helper);
+ #endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && PACKAGE_AS
Proguard (optional)
- Edit
project.properties
to specify aProguard
configuration file. Example:
proguard.config=proguard.cfg
- Edit the file you specified to include the following:
# 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.