‹ Misc Doc Home

Misc 集成向导

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

集成

打开终端, 在其中输入以下命令来安装 SDKBox Misc 插件.

$ sdkbox import misc

iOS额外修改

文件改动列表:

Note: 以下所有改动都以 diff 形式展示相应的修改, 在不同的版本上可能会有细微差异


  #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 {

Android 额外修改

文件改动列表:

  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 配置

SDKBox 安装会自动插件一个配置信息到 sdkbox_config.json 中, 目前只需要保持 Misc 配置为空就好, 如下:


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

用法

初始化 Misc

在你的代码中初始化插件, 我们推荐在 AppDelegate::applicationDidFinishLaunching()AppController:didFinishLaunchingWithOptions() 中初始化. 请注意包含了相应的头文件:

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

发送本地消息

// 10 秒后能收到消息
int nid = sdkbox::PluginMisc::localNotify("test title", "this a test notify content", 1000 * 10);
std::stringstream buf;
buf << "Local Notification:" << nid;
CCLOG(buf.str());

实现 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 文档

方法

static bool init ( ) ;

初始化插件

static void setListener ( MiscListener * listener ) ;

设置插件的 Listener

static MiscListener * getListener ( ) ;

取插件的 Listener

static void removeListener ( ) ;

删除 Listener

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

发送一个本地消息 @return 消息的id

Listeners

void onHandleLocalNotify ( const std::string & payloadJson ) 

手动集成

如果 SDKBOX 安装器 安装插件失败了,那么需要手动集成插件.如果安装器安装插件成功了,那么不需要,也没必要,按文档再手动集成一次.

下面列出的的步骤一般很少用到.如果你按下面的步骤完成了集成,请在完成集成后,再按步骤检查一次.

iOS 工程的手动集成

将如下 framework 拷到 iOS 工程目录下,并将它添加到 Xcode 中

sdkbox.framework

PluginMisc.framework

如果你的工程中没有包含如下系统 framework, 你还需要添加它们:

SystemConfiguration.framework

以下是文件修改列表:


  #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 {

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 (release模式下可选)

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 只能工作在 Release 模式下 (比如: cocos run -m release) debug 模式下不会触发 Proguard 。