用coocs2dx引擎开发单机,或者若联网游戏,加入推送通知是很有必要的。现在把本人在项目中做的推送通知整理一下供大家参考,抛砖引玉,我是调用的android 和 ios 平台自己的推送。
首先是一个管理者:
#ifndef __CCNOTIFICATION_MANAGER_H__
#define __CCNOTIFICATION_MANAGER_H__
#define NM CCNotificationManager::getNotificationManager()
#include
class
{
public:
private:
private:
};
#include
#include
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include
#endif
CCNotificationManager*
CCNotificationManager::CCNotificationManager()
{
}
CCNotificationManager*
{
}
void
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
//
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#endif
}
#endif
ios 下 的实现。
#ifndef __CCNOTIFICATION_HELPER_H__
#define __CCNOTIFICATION_HELPER_H__
#include
#include
#include
class
{
public:
private:
private:
};
#define CNH
#endif
#include
CCNotificationHelper*
CCNotificationHelper*
{
}
void
{
}
void
{
}
CCNotificationHelper::CCNotificationHelper()
{
}
android下的实现
#ifndef __CCNOTIFICATION_ANDROID_H__
#define __CCNOTIFICATION_ANDROID_H__
#include
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include
extern"C"
{
}
#endif
#endif
#include
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include
#include
#include
extern"C"
{
}
#endif
然后切换到pro.android
public
public
public
publicstatic
protected
Log.e("com.ea.product.alpaca",
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
malpacaObj
}
public Cocos2dxGLSurfaceView onCreateView() {
Log.e("com.ea.product.alpaca",
Cocos2dxGLSurfaceView glSurfaceView =
//
glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
return
}
@Override
protected
super.onActivityResult(requestCode, resultCode, data);
}
//Serializeable传递对象的方法
public
Intent intent =
intent.setClass(alpaca.this, CCNotifitionService.class);
Bundle mBundle =
mBundle.putSerializable(SER_KEY, message);
intent.putExtras(mBundle);
alpaca.this.startService(intent);
System.out.println("Push notify message.");
}
public static void pushMessage(String message,long mark,int repeats) {
System.out.println("alpaca.passFromJni()"+mark);
NotificationMessage nmObj =
nmObj.setMessage(message);
nmObj.setMark(mark *
nmObj.setId(repeats);
malpacaObj.SerializeMethod(nmObj);
}
public
super.onStop();
}
public
super.onDestroy();
}
public
}
static
System.loadLibrary("alpaca");
}
}
这个是一个实体model
package
import
public
private
privatelong
privateint
public
returnmessage;
}
public
this.message
}
public
returnmark;
}
public
this.mark
}
public
returnid;
}
public
this.id
}
public
super();
this.message
this.mark
this.id
}
public
}
}
最后在service中做逻辑处理(启动一个定时器到时间就推送)
package com.ea.product.alpaca;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import com.ea.product.alpaca.alpaca;
import android.app.NotificationManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
public class CCNotifitionService extends Service {
static List mMessageList = new ArrayList();
String mmessage = "" ;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public CCNotifitionService(){
System.out.println("CCNotifitionService.CCNotifitionService()");
}
@Override
public void onCreate()
{
System.out.println("onCreate()"+mMessageList.size());
super.onCreate();
Timer timer = new Timer(true);
timer.schedule(
new java.util.TimerTask()
{
public void run() {
if(checkPushStatue())
{
showNotification();
}
}
}, 0, 1*1000);
}
@Override
public void onStart(Intent intent, int startId)
{
if((intent!=null) )
{
NotificationMessage message = (NotificationMessage)intent.getSerializableExtra(alpaca.SER_KEY);
mMessageList.add(message);
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
public CCNotifitionService(Context context){
}
public boolean checkPushStatue(){
long currentTime = System.currentTimeMillis();
if(mMessageList.size()>0)
{
int listSize = mMessageList.size();
for(int i = 0 ;i
if(mMessageList.get(i).getMark() < currentTime)
{
mmessage = mMessageList.get(i).getMessage();
mMessageList.remove(i);
return true;
}
else
{
return false;
}
}
}
else
{
return false;
}
return false;
}
public void showNotification()
{
Notification notification = new Notification(R.drawable.icon, mmessage, System.currentTimeMillis());
notification.defaults = Notification.DEFAULT_SOUND;
Intent it = new Intent(Intent.ACTION_MAIN);
it.setClassName(this,"com.ea.product.alpaca.alpaca");
it.addCategory(Intent.CATEGORY_LAUNCHER);
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
PendingIntent pintent = PendingIntent.getActivity(CCNotifitionService.this, 0,it,0);
notification.setLatestEventInfo(CCNotifitionService.this,"",mmessage,pintent);
String service = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager =(NotificationManager)getSystemService(service);
mNotificationManager.notify(R.string.app_name, notification);
}
public class PushThread implements Runnable {
public void run() {
System.out.println("CCNotifitionService.PushThread.run()"+mMessageList.size());
while(true)
{
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(checkPushStatue())
{
showNotification();
}
}
}
}
}
最后千万不能忘了在配置文件中加入这个service的属性