cocos2dx 本地推送
发布时间:2020-12-14 21:06:22 所属栏目:百科 来源:网络整理
导读:以 http://blog.sina.com.cn/s/blog_62f189570101bw5x.html 的基础上进行修改完善的。 由于项目的需求,需要将推送做到客户端。用的引擎是cocos2dx的。但是我看了下,大概思路还是安卓普遍都可以使用的。 大致思路就是建立一个service,然后在service中不断
以
http://blog.sina.com.cn/s/blog_62f189570101bw5x.html
的基础上进行修改完善的。
由于项目的需求,需要将推送做到客户端。用的引擎是cocos2dx的。但是我看了下,大概思路还是安卓普遍都可以使用的。
大致思路就是建立一个service,然后在service中不断的去判断当前时间是否达到了指定时间,打到了指定时间就用安卓的notification进行推送。
首先在c++中定义一个Notification的管理类:
void notification(const char * message,int delay,int repeats,const char * key); 的作用就是将需要的类容,时间等 发送到java那边进行存储。
key 这个字段可以不管,这是ios需要使用的,现在暂时不需要管。(我是在上面那个地址的内容上进行修改的,也可以单独区分ios 和 安卓的)
"CCNotificationManager.h"#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)"CCNotificationHelper.h"#elif(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)"CCNotificationAndroid.h"#endifCCNotificationManagerm_pNotifiMgr = NULL;"..PlayerFmLocalPlayer.h"CCNotificationManager::CCNotificationManager(){}CCNotificationManager{ifNULL ) m_pNotifiMgr newreturn;}}notification){#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) // CNH->pushMessage(message,delay,repeats,key ); CNH->pushMessagemessage 0);long time GetLocalPlayer()->getCurrentTime java_push#endif}
iso的展示不需要去看。
时间的获取是这样的:
然后看java_push
__CCNOTIFICATION_ANDROID_H__"SDK/GlobalHead.h"#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)<jni.h>extern"C"externsrcint rep java_removePush();}#endif#endif
这里,本来是想着用直接把long类型的时间用jni传递到java里面直接使用,但是这样的话,前面的获取时间的方法需要修改下。我这里的方法是直接传入delay的时间。(原谅我对Jni 和java 不熟悉)
然后是java 部分:
在主activity中实现:
我这里的message 传入的是安卓Notification 需要用到的title 和 info,我用title#info 传入。
这里就是开启我们的推送服务
存储数据的类:
implements Serializableprivatefinal serialVersionUID -7060210544600464481L mark id title getMessage getTitle setTitle){thistitle setMessagemessage getMark setMarkmark getId setIdid super}
然后看推送的service
NotifitionServiceextends Service List<NotificationMessage> mMessageList ArrayList>(); mMessage "" mTitle @Override IBinder onBind(Intent// TODO Auto-generated method stubnull onStartCommand flags startIdonStartCommand onCreateonCreate Timer timer Timertrue timerschedule javautil.TimerTask(){ runcheckPauseState())ifisAppOnForeground showNotification},11000 onStartonStartintent != notimessage (NotificationMessagegetSerializableExtra(BigHero notimessagegetMark mMessageListaddnotimessageboolean checkPauseStateoutprintln"NotifitionService=========="curtimemMessageListsize listsize for i < listsize i++)geti).*1000&&-getMessagegetTitleremove Notification notification Notificationicon Rdrawableic_launchertickerText mMessagewhen defaults DEFAULT_SOUNDflags |=FLAG_AUTO_CANCELACTION_MAINsetClassName"org.cocos2dx.bighero.BigHero"addCategoryCATEGORY_LAUNCHERsetFlagsFLAG_ACTIVITY_NEW_TASK |FLAG_ACTIVITY_RESET_TASK_IF_NEEDED PendingIntent pintent PendingIntentgetActivity(NotifitionServicesetLatestEventInfomTitlemMessagepintent service ContextNOTIFICATION_SERVICE NotificationManager mNotificationManager =(NotificationManagergetSystemServiceservice mNotificationManagernotifyRstringapp_name PushThread Runnable"CCNotifitionService.PushThread.run()"());whiletry Threadsleep(catch(InterruptedException e// TODO: handle exceptionprintStackTrace/** * 程序是否在前台运行 * * @return */ isAppOnForeground// Returns a list of application processes that are running on the // device ActivityManager activityManager (ActivityManager getApplicationContext().(ContextACTIVITY_SERVICE packageName getPackageName<RunningAppProcessInfo appProcesses activityManager getRunningAppProcessesappProcesses (RunningAppProcessInfo appProcess appProcesses// The name of the process that this object is associated with. appProcessprocessNameequalspackageName appProcessimportance RunningAppProcessInfoIMPORTANCE_FOREGROUND}
更多的可以去看看Notification的实现
最后是AndroidManifest 中添加服务
<service android:name="NotifitionService"
android:process=":system" >
</service>
请注意,这里的提升进程的级别是:system 而不是system ,网上一堆的system 把我害惨了。。。。。
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |