coocs2dx本地推送


用coocs2dx引擎开发单机,或者若联网游戏,加入推送通知是很有必要的。现在把本人在项目中做的推送通知整理一下供大家参考,抛砖引玉,我是调用的android 和 ios 平台自己的推送。

首先是一个管理者:

#ifndef __CCNOTIFICATION_MANAGER_H__

#define __CCNOTIFICATION_MANAGER_H__

 

#define NM CCNotificationManager::getNotificationManager()

 

#include "GlobalHead.h"

 

class CCNotificationManager

{

    

public:

    static CCNotificationManager* getNotificationManager();

    

    void notification(const char * message,long delay,int repeats,const char * key);

private:

    CCNotificationManager();

 

private:

    staticCCNotificationManager* m_pNotifiMgr;

    

};

 

 

#include "CCNotificationManager.h"

#include "CCTimeHelper.h"

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

#include "CCNotificationHelper.h"

#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

#include "CCNotificationAndroid.h"

#endif

 

CCNotificationManager* CCNotificationManager::m_pNotifiMgr = NULL;

CCNotificationManager::CCNotificationManager()

{

   

}

 

CCNotificationManager* CCNotificationManager::getNotificationManager()

{

    if (NULL == m_pNotifiMgr)

    {

        m_pNotifiMgr = newCCNotificationManager();

        returnm_pNotifiMgr;

    }

    returnm_pNotifiMgr;

}

 

void CCNotificationManager::notification(const char * message,long delay,int repeats,const char * key)

{

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    

//  CNH->pushMessage(message ,delay ,repeats,key );

    CNH->pushMessage(message ,delay ,0,key );

 

#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

 

    java_push(message,delay + GTH->getCurrentTime(),repeats);

    

#endif

}

 

 

 

#endif 

 

 

 

ios 下 的实现。 

#ifndef __CCNOTIFICATION_HELPER_H__

#define __CCNOTIFICATION_HELPER_H__

 

#include 

#include 

#include "GlobalHead.h"

 

class  CCNotificationHelper

{

public:

    static CCNotificationHelper* sharedInstance();

    

    void pushMessage(const char * message,long delay,int repeats,const char * key);

    

    void removeNotification();

    

private:

    CCNotificationHelper();

    

private:

    static CCNotificationHelper m_pNotification;

    bool   m_accept;

};

 

#define CNH     CCNotificationHelper::sharedInstance()

 

#endif 

 

 

 

#include "CCNotificationHelper.h"

 

CCNotificationHelper* CCNotificationHelper::m_pNotification = NULL;

 

 

CCNotificationHelper* CCNotificationHelper::sharedInstance()

{

    if (!m_pNotification)

    {

        m_pNotification = newCCNotificationHelper();

        returnm_pNotification;

    }

    

    returnm_pNotification;

 

}

 

void CCNotificationHelper::pushMessage(const char * message,long delay,int repeats,const char * key)

{

    // repeats  0:不重复 1:/DAY  2:/HOUR 3:/MINUTE 4:SECOND

    

    NSString * castStr = [[NSStringalloc] initWithUTF8String:message];

    

    NSString * keyStr = [[NSStringalloc] initWithUTF8String:key];

    

    //1、增加一个本地推送

    NSDate *date = [NSDatedateWithTimeIntervalSinceNow:delay];

    //创建一个本地推送

    UILocalNotification *noti = [[[UILocalNotificationalloc] init] autorelease];

    if (noti) {

 

        //设置推送时间

        noti.fireDate = date;

        //设置时区

        noti.timeZone = [NSTimeZonedefaultTimeZone];

        //设置重复间隔

        if(repeats == 1)

        {

            noti.repeatInterval  = kCFCalendarUnitDay;

        }else if (repeats == 2)

        {

            noti.repeatInterval  = kCFCalendarUnitHour;

        }else if (repeats == 3)

        {

            noti.repeatInterval  = kCFCalendarUnitMinute;

        }else if (repeats == 4)

        {

            noti.repeatInterval  = kCFCalendarUnitSecond;

        }else

        {

            noti.repeatInterval  = 0;

        }

        

        //推送声音

        noti.soundName = UILocalNotificationDefaultSoundName;

        //内容

        noti.alertBody = castStr;

        

        //        //显示在icon上的红色圈中的数子

        noti.applicationIconBadgeNumber = 0;

        

        //设置userinfo 方便在之后需要撤销的时候使用

        NSDictionary *infoDic = [NSDictionarydictionaryWithObject:keyStr forKey:keyStr];

        noti.userInfo = infoDic;

        //添加推送到uiapplication

        UIApplication *app = [UIApplicationsharedApplication];

        [app scheduleLocalNotification:noti];

    }

 

    [castStr release];

    

    [keyStr release];

    

}

 

void CCNotificationHelper::removeNotification()

{

    [[UIApplicationsharedApplication] cancelAllLocalNotifications];

}

 

CCNotificationHelper::CCNotificationHelper()

{

    

}

 

 

android下的实现

 

#ifndef __CCNOTIFICATION_ANDROID_H__

#define __CCNOTIFICATION_ANDROID_H__

 

#include "GlobalHead.h"

 

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

#include   

 

extern"C"

{

    extern void java_push(string message,long mark,int repeats);

    

    extern void java_removePush();

}

#endif

 

#endif

 

#include "CCNotificationAndroid.h"

 

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

#include "jni/JniHelper.h"

#include 

#include 

 

extern"C"

{

    void java_push(string message,long mark,int repeats)

    {

        JniMethodInfo t;

        //LI

        if (JniHelper::getStaticMethodInfo(t,"com/ea/product/alpaca/alpaca","pushMessage","(Ljava/lang/String;JI)V"))

        {

            jstring messageStr = t.env->NewStringUTF(message.c_str());

            t.env->CallStaticVoidMethod(t.classID,t.methodID,messageStr,mark,repeats);

            t.env->DeleteLocalRef(messageStr);

        }

    }

    

    void java_removePush()

    {

        

        JniMethodInfo minfo;

        bool isHave = JniHelper::getStaticMethodInfo(minfo,"com/ea/product/alpaca/alpaca","removeNotification", "()V");

        if (!isHave)

        {

            CCLOG("jni:此函数不存在");

        }

        else

        {

            minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID);

        }

    }

}

#endif

 

然后切换到pro.android 

public class alpaca extends Cocos2dxActivity {

 

public  finalstaticint  convert = 1000;

public  finalstatic String SER_KEY = "com.ea.product.alpaca.message";

publicstatic alpaca malpacaObj;

 

protected void onCreate(Bundle savedInstanceState) {

Log.e("com.ea.product.alpaca", "onCreate");

super.onCreate(savedInstanceState);

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

malpacaObj = this;

public Cocos2dxGLSurfaceView onCreateView() {

Log.e("com.ea.product.alpaca", "onCreateView");

Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);

// alpaca should create stencil buffer

glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);

 

return glSurfaceView;

}

 

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

}

//Serializeable传递对象的方法  

public void SerializeMethod(NotificationMessage message) {

Intent intent = new Intent();

intent.setClass(alpaca.this, CCNotifitionService.class);

Bundle mBundle = new Bundle();

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 = new NotificationMessage();

nmObj.setMessage(message);

nmObj.setMark(mark * convert);

nmObj.setId(repeats);

malpacaObj.SerializeMethod(nmObj);

}

 

public void onStop() {

super.onStop();

}

 

public void onDestroy() {

super.onDestroy();

}

 

public alpaca() {

}

 

static {

System.loadLibrary("alpaca");

}

 

}

 

这个是一个实体model

package com.ea.product.alpaca;

 

import java.io.Serializable;

 

public class NotificationMessage implements Serializable{

    private static final long serialVersionUID = -7060210544600464481L;   

 

private String message;

privatelong   mark;

privateint    id;

public String getMessage() {

returnmessage;

}

public void setMessage(String message) {

this.message = message;

}

public long getMark() {

returnmark;

}

public void setMark(long mark) {

this.mark = mark;

}

public int getId() {

returnid;

}

public void setId(int id) {

this.id = id;

}

public NotificationMessage(String message, long mark, int id) {

super();

this.message = message;

this.mark = mark;

this.id = id;

}

 

public NotificationMessage() {

 

}

 

 

}

 

 

最后在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的属性

        <</SPAN>serviceandroid:enabled="true"android:name=".CCNotifitionService"

            android:process=":message"

            />

 这样就可以在用户退出后,开启一个新的进程来处理逻辑(判断满足条件推送)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VR(Virtual Reality)即虚拟现实,是一种可以创建和体验虚拟世界的计算机技术。它利用计算机生成一种模拟环境,是一种多源信息融合的、交互式的三维动态视景和实体行为的系统仿真,使用户沉浸到该环境中。VR技术通过模拟人的视觉、听觉、触觉等感觉器官功能,使人能够沉浸在计算机生成的虚拟境界中,并能够通过语言、手势等自然的方式与之进行实时交互,创建了一种适人化的多维信息空间。 VR技术具有以下主要特点: 沉浸感:用户感到作为主角存在于模拟环境中的真实程度。理想的模拟环境应该使用户难以分辨真假,使用户全身心地投入到计算机创建的三维虚拟环境中,该环境中的一切看上去是真的,听上去是真的,动起来是真的,甚至闻起来、尝起来等一切感觉都是真的,如同在现实世界中的感觉一样。 交互性:用户对模拟环境内物体的可操作程度和从环境得到反馈的自然程度(包括实时性)。例如,用户可以用手去直接抓取模拟环境中虚拟的物体,这时手有握着东西的感觉,并可以感觉物体的重量,视野中被抓的物体也能立刻随着手的移动而移动。 构想性:也称想象性,指用户沉浸在多维信息空间中,依靠自己的感知和认知能力获取知识,发挥主观能动性,寻求解答,形成新的概念。此概念不仅是指观念上或语言上的创意,而且可以是指对某些客观存在事物的创造性设想和安排。 VR技术可以应用于各个领域,如游戏、娱乐、教育、医疗、军事、房地产、工业仿真等。随着VR技术的不断发展,它正在改变人们的生活和工作方式,为人们带来全新的体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值