{"id":20732409,"url":"https://github.com/jpush/jpush-cocos2d-x-plugin","last_synced_at":"2025-04-23T22:09:01.429Z","repository":{"id":16054968,"uuid":"18799103","full_name":"jpush/jpush-cocos2d-x-plugin","owner":"jpush","description":"JPush's officially supported Cocos2d-x plugin (Android \u0026 iOS). 极光推送官方支持的 Cocos2d-x 插件（Android \u0026 iOS）。","archived":false,"fork":false,"pushed_at":"2016-07-06T06:41:29.000Z","size":17392,"stargazers_count":18,"open_issues_count":1,"forks_count":13,"subscribers_count":25,"default_branch":"master","last_synced_at":"2025-04-23T22:08:08.048Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://docs.jpush.io","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jpush.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-04-15T12:26:51.000Z","updated_at":"2024-02-19T05:34:09.000Z","dependencies_parsed_at":"2022-09-06T01:22:02.641Z","dependency_job_id":null,"html_url":"https://github.com/jpush/jpush-cocos2d-x-plugin","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpush%2Fjpush-cocos2d-x-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpush%2Fjpush-cocos2d-x-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpush%2Fjpush-cocos2d-x-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpush%2Fjpush-cocos2d-x-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jpush","download_url":"https://codeload.github.com/jpush/jpush-cocos2d-x-plugin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250522299,"owners_count":21444511,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-17T05:19:15.359Z","updated_at":"2025-04-23T22:09:01.382Z","avatar_url":"https://github.com/jpush.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jpush-cocos2d-x-plugin\n\n[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/jpush/jpush-phonegap-plugin)\n[![platforms](https://img.shields.io/badge/platforms-iOS%7CAndroid-lightgrey.svg)](https://github.com/jpush/jpush-cocos2d-x-plugin)\n[![weibo](https://img.shields.io/badge/weibo-JPush-blue.svg)](http://weibo.com/jpush?refer_flag=1001030101_\u0026is_all=1)\n\nJPush's officially supported Cocos2d-x plugin (Android \u0026amp; iOS). 极光推送官方支持的 Cocos2d-x 插件（Android \u0026amp; iOS）。\n\n\n## iOS Project 集成 JPush 插件\n\n#### 1. 配置基本信息\n\n* 使用 Cocos2d-x 生成 iOS 工程\n\n* 添加必要框架。打开 Xcode，点击 project，选择 (Targets -\u003e Build Phases -\u003e Link Binary With Libraries)，添加以下框架：\n\n\t\tCFNetwork.framework\n\t\tCoreFoundation.framework\n\t\tCoreTelephony.framework\n\t\tCoreGraphics.framework\n\t\tFoundation.framework\n\t\tUIKit.framework\n\t\tSecurity.framework\n\t\tSystemConfiguration.framework\n\t\tlibz.tbd//Xcode7 以下是 libz.dylib\n\t\tAdSupport.framework//若需要使用 IDFA 广告标识符则添加该库\n\n* 将插件的 /iOS/JPushPlugin 文件夹及内容拖拽到 Xcode 工程里，拖拽时 Choose options for adding these files 选择：\n\t-  Destination：✓ Copy items if needed\n\t-  Added folders：✓ Create groups\n\t-  Add to targets：✓ your-proj-name\n  \n#### 2. 添加代码\n\n* \t在工程的 /ios/AppController.mm (注意不是 AppDelegate.cpp) 中添加头文件：\n\n\t\t#import \"JPUSHService.h\"\n\t\t//#import \u003cAdSupport/AdSupport.h\u003e//如需使用广告标识符 IDFA 则添加该头文件，否则不添加\n\n\n* 在 AppController.mm 中添加以下代码：（如果方法存在，则只将其中代码添加至方法中；如果方法不存在，则添加方法及其中代码）\n\n\t```\n\t- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{\n\t\t\n\t\t\n\t\t// 注册推送\n\t\t#if __IPHONE_OS_VERSION_MAX_ALLOWED \u003e __IPHONE_7_1\n\t\t    if ([[UIDevice currentDevice].systemVersion floatValue] \u003e= 8.0) {\n\t\t        //可以添加自定义categories\n\t\t        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |\n\t\t                                                       UIUserNotificationTypeSound |\n\t\t                                                       UIUserNotificationTypeAlert)\n\t\t                                           categories:nil];\n\t\t        } else {\n\t\t            //categories 必须为nil\n\t\t            [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |\n\t\t                                                           UIRemoteNotificationTypeSound |\n\t\t                                                           UIRemoteNotificationTypeAlert)\n\t\t                                               categories:nil];\n\t\t        }\n\t\t#else\n\t\t        //categories 必须为nil\n\t\t        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |\n\t\t                                                       UIRemoteNotificationTypeSound |\n\t\t                                                       UIRemoteNotificationTypeAlert)\n\t\t                                           categories:nil];\n\t\t#endif\n\t\t\n\t\t//启动 sdk\n\t\t/* （1）不使用 IDFA 启动 sdk\n\t\t\t参数说明：\n            \tappKey：极光官网控制台应用标识\n            \tchannel：频道，暂无可填任意\n            \tapsForProduction：YES发布环境/NO开发环境\n\t\t*/\n\t\t[JPUSHService setupWithOption:launchOptions appKey:@\"abcacdf406411fa656ee11c3\" channel:@\"\" apsForProduction:NO];\n\n\n\t\t/* （2）使用 IDFA 启动 sdk （不与上述方法同时使用）\n\t\t\t参数说明：\n\t\t\t\tappKey：极光官网控制台应用标识\n\t\t\t\tchannel：频道，暂无可填任意\n\t\t\t\tapsForProduction：YES发布环境/NO开发环境\n\t\t\t\tadvertisingIdentifier：IDFA广告标识符\n\t\t*/\n\t\t//NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];\n\t\t//[JPUSHService setupWithOption:launchOptions appKey:@\"abcacdf406411fa656ee11c3\" channel:@\"\" apsForProduction:NO advertisingIdentifier:advertisingId];\n\n\n    \t......\n\t    return YES;\n\t}\n\t```\n\t```\n\t- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{\n\t\t// Required\n\t\t[JPUSHService registerDeviceToken:deviceToken];\n\t}\n\t```\n\t```\t\n\t- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {\n\t  \t// Required\n\t\t[JPUSHService handleRemoteNotification:userInfo];\n\t}\n\t```\n\t```\n    //IOS7 only\n\t- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {\n  \t\t[JPUSHService handleRemoteNotification:userInfo];\n\t  \tcompletionHandler(UIBackgroundFetchResultNewData);\n\t}\n\t```\n\t\n* 在需要处理推送回调的类中添加回调函数，相应地调用 JPush SDK 提供的 API 来实现功能,调用地方需要引入头文件 JPushBridge.h\n\n\t\t#import \"JPushBridge.h\"\n\n\t\tJPushBridge::registerCallbackFunction(setupCallback, closeCallback,\n                                         Register_callback, Login_callback,\n                                         ReceiveMessage_callback);\n                                         \n                                         \n* 或者你也可以分别调用每一个回调函数的设置 API 方法\n\n\t\tstatic void registerSetupCallbackFunction(setupCallback);\n\t\tstatic void registerCloseCallbackFunction(closeCallback);\n\t\tstatic void registerRegisterCallbackFunction(Register_callback);\n\t\tstatic void registerLoginCallbackFunction(Login_callback)\n\t\tstatic void registerCallbackFunction(ReceiveMessage_callback);\n\n* API 参数要符合头文件提供的函数指针\n\n\t\tvoid setupCallback() { cout \u003c\u003c \"setup\" \u003c\u003c endl; }\n\n* Tags、Alias 设置方法,自定义 tagsAliasCallback 要符合头文件的函数指针\n\n\t\tJPushBridge::setAliasAndTags(\"别名1\", tags1, tagsAliasCallback);\n\t\t\n* Tags 过滤方法：需要传入一个 result 指针用以获取过滤后的 Tags. \n\n\t\tbool filterValidTags(set\u003cstring\u003e *tags, set\u003cstring\u003e *result);\n\n* 获取 RegistrationID\n\n\t\tvoid register_callback(const char *registrationID)；\n\t\t\n\n\n## Android \u0026 Android Studio Project 集成 JPush 集成\n\n\n#### 执行自动安装脚本\n\n- 将工程文件 `YourProjectName` 置于 cocos2d-x-3.x `/projects/` 目录下\n\n- 将插件 `jpush-cocos2d-x-plugin` 文件夹置于 ocos2d-x-3.x `/plugin/plugins/` 目录下。\n\n- 使用命令行工具，进入插件目录 `/jpush-cocos2d-x-plugin/Android/`执行自动安装脚本\n\t- Android 工程（pro.android）执行 install_android.py\n\t\n\t\t\t./install_android.py -project YourProjectName -package YourPackageName -appkey YourAppkey\n\t\t\n\n\t- Android Studio 工程（proj.android-studio）执行 install_android_studio.py\n\t\n\t\t\t./install_android_studio.py -project YourProjectName -package YourPackageName -appkey YourAppkey\n\n\t\n\t显示 `JPush SDK installed successfully,have fun!` 则安装成功！\n\t\n- 因目录结构不同可能导致安装失败，关键位置目录关系参照如下(以 cocos2d-x-3.10 为例)：\n\t\t\n\t\t\t../Cocos2d-x/cocos2d-x-3.10/\n\t\t\t├─┬ /plugin/plugins/jpush-cocos2d-x-plugin/Android/\n\t\t\t│ ├── install_android.py\n\t\t\t│ └── install_android_studio.py\n\t\t\t└─┬ /projects/YourProjectName/\n\t\t\t  ├── /Classes/\n\t\t\t  ├─┬ /proj.android/\n\t\t\t  │ ├── /jni/\n\t\t\t  │ ├── /libs/\n\t\t\t  │ └── /src/\n\t\t\t  └─┬ /proj.android-studio/app/\n\t\t\t    ├── /jni/\n\t\t\t    ├── /libs/\n\t\t\t    └── /src/\n\t- 因 Cocos2d-x 版本不同导致目录变化，可相应的对自己的目录进行调整，以便成功安装\n\t- 自动安装失败时可以尝试 [手动安装](https://github.com/jpush/jpush-cocos2d-x-plugin/issues/1)\n\t\t\t    \n#### 使用 API\n\nJPush SDK 提供的 API 接口,都主要集中在 JPushBridge.h 类里。只需要在第一个游戏场景中：\n\n- init 初始化 SDK\n\n\t\tJPushBridge::init();\n\t\t\n- setDebugMode 设置调试模式\n\n\t\t// You can enable debug mode in developing state. You should close debug mode when release.\n\t    JPushBridge::setDebugMode(true);\n\t   \n就可以使用推送消息了。\n\n#### 接收推送消息\n这个动作不是必须的，如果不做这个动作，则默认的行为是：\n\n- 接收到推送的自定义消息，则没有被处理\n- 可以正常收到通知，用户点击打开应用主界面\n\n\n**1.** 如果全部类型的广播都接收，则需要在 AndroidManifest.xml 里添加如下的配置信息：\n\n\t\u003creceiver android:name=\"JPushReceiver\" android:enabled=\"true\"\u003e\n\t    \u003cintent-filter\u003e\n\t        \u003caction android:name=\"cn.jpush.android.intent.REGISTRATION\" /\u003e\n\t        \u003caction android:name=\"cn.jpush.android.intent.MESSAGE_RECEIVED\" /\u003e\n\t        \u003caction android:name=\"cn.jpush.android.intent.NOTIFICATION_RECEIVED\" /\u003e\n\t        \u003caction android:name=\"cn.jpush.android.intent.NOTIFICATION_OPENED\" /\u003e\n\t        \u003ccategory android:name=\"You Package Name\" /\u003e\n\t    \u003c/intent-filter\u003e\n\t\u003c/receiver\u003e\n将 `Your Package Name` 替换成你自己的包名。\n\n**2.** 将 SDK 中的 `Android/JPushReceiver.java` 放在 `src` 包名目录下.\n\n**3.** 注册回调函数\n\n首先定义一个回调函数，如：\n\n\tvoid handlerRemoteNotification(void* p_handler,const char *message){\n\t\t//当收到推送通知时，会触发这个回调函数，其中message参数是一个Json字符串，你可以\n\t\t//从中获取通知的详细信息\n\t}\n然后调用\n\t   \n\tJPushBridge::registerRemoteNotifcationCallback(this, \u0026handlerRemoteNotification);\n\t \n向 JPushBridge 注册此回调函数，具体字段可参考 `JPushReceiver.java` 类。\n\n#### 测试确认\n1. 确认所需的权限都已经添加。如果必须的权限未添加,日志会提示错误。\n2. 确认 AppKey (在 Portal 上生成的)已经正确的写入 Androidmanifest.xml 。\n3. 确认在程序启动时候调用了 init() 接口\n4. 确认测试手机(或者模拟器)已成功连入网络\n客户端调用 init 后不久,如果一切正常,应有登录成功的日志信息\n5. 启动应用程序,在 Portal 上向应用程序发送自定义消息或者通知栏提示。详情请参考管理 Portal。\n在几秒内,客户端应可收到下发的通知或者正定义消息.\n\n#### 常见问题\n\n##### multiple definition of 'getCallbackHelperObject\n\n* 检查文件 **jni/Android.mk** 中 **LOCAL_SRC_FILES :** 是否重复包含 **JPushBridge.cpp**\n\n#####如何升级 Cocos2d-x plugin for Android 插件\t\t\n* 将 `{COCOS2DX_ROOT}/plugin/plugins/jpush-cocos2d-x-plugin` 文件夹删除，再按照上面的集成文档执行install_jpush.py脚本即可\t\t\n\n##### C++ 接口的怎么调用？\n* C++ 的函数名称与 java 方法相对应，具体请参照[JPush文档: Android 的 API](http://docs.jpush.cn/display/dev/API%3A+Android)\n\n##### 在 Android 的工程中加了其他的 SDK 时，重新编译时，其他 SDK 的 so 文件消息怎么办？\n\n引起的原因:是因为 libs/armeabi 目录大小发生较大的变化时，每次编译会更新这个目录，导致我们的第三方库被删除\n\n解决方案：\n\n1. 在 [your_android_project]/jni/preduild/Android.mk 文件中 加入\n\t\t\n\t\tinclude $(CLEAR_VARS)\n\t\tLOCAL_MODULE := your_module\n\t\tLOCAL_SRC_FILES := your_project.so\n\t\tinclude $(PREBUILT_SHARED_LIBRARY)\n\n2. 在 [your_android_project]/jni/Andorid.mk 中找到 `LOCAL_SHARED_LIBRARIES := jpush_so\n` 将其修改成：\n\n\t\tLOCAL_SHARED_LIBRARIES := jpush_so your_project_so\n\n\n\n## 高级功能 \n请参考:\n\n[Android 标签与别名 API](http://docs.jpush.cn/pages/viewpage.action?pageId=557241)\n[Android 接收推送消息](http://docs.jpush.cn/pages/viewpage.action?pageId=1343602)\n\n[iOS 标签与别名 API](http://docs.jpush.cn/pages/viewpage.action?pageId=3309913)\n\n[iOS 接收推送消息](http://docs.jpush.cn/pages/viewpage.action?pageId=3310013)\n\n## 技术支持\n邮件联系: \u003csupport@jpush.cn\u003e \n\n极光社区(答疑论坛): [http://community.jpush.cn/](http://community.jpush.cn/)\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjpush%2Fjpush-cocos2d-x-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjpush%2Fjpush-cocos2d-x-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjpush%2Fjpush-cocos2d-x-plugin/lists"}