‹ Misc Doc Home

Misc 集成向导

For the Javascript 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

调用 init() 可以初始化插件, 我们建议在 app.js 中初始化插件.

sdkbox.PluginMisc.init();

发送一个本地消息

// 10 秒后会收到一个消息
let nid = sdkbox.PluginMisc.localNotify("test title", "this a test notify content", 1000 * 10);
cc.log('Local Notification ID:' + nid);

实现 MiscListner


sdkbox.PluginMisc.setListener({
    onHandleLocalNotify :function (json) {
        cc.log(json);
    }
});

API 文档

方法

sdkbox.PluginMisc.init();

初始化插件

sdkbox.PluginMisc.setListener(listener);

设置 Listener

sdkbox.PluginMisc.localNotify(title, content, delaymillisecond);

发送本地消息 @return 消息id

Listeners

onHandleLocalNotify (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" />

如果你使用的是 creator 1.7+ 的话,那么你应该拷贝 jsb2 文件夹中的 js 绑定文件:

                   $(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

注册 js

  #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 (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 。