cocos2d-x中本地推送消息
作者:HU
转载请注明,原文链接:http://www.cnblogs.com/xioapingguo/p/4038277.html
IOS下很简单:
添加一条推送
void PushNotificationIOS::addNoticfy(std::string title,std::string content,unsigned int delalt,std::string key,unsigned int repeatTime)
{ // 创建一个本地推送//
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
//设置delalt秒之后//
NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:delalt];
if (notification != nil)
{
// 设置推送时间//
notification.fireDate = pushDate;
// 设置时区//
notification.timeZone = [NSTimeZone defaultTimeZone];
// 设置重复间隔//
if (repeatTime!=)
{
notification.repeatInterval = kCFCalendarUnitDay;
}
else
{
notification.repeatInterval = ;
}
// 推送声音//
notification.soundName = UILocalNotificationDefaultSoundName;
// 推送内容//
notification.alertBody = [NSString stringWithUTF8String: content.c_str()];
//显示在icon上的红色圈中的数子//
notification.applicationIconBadgeNumber = ;
//设置userinfo 方便在之后需要撤销的时候使用//
NSDictionary *info = [NSDictionary dictionaryWithObject:[NSString stringWithUTF8String: key.c_str()] forKey:@"DDNoticfykey"];
notification.userInfo = info;
//添加推送到UIApplication//
UIApplication *app = [UIApplication sharedApplication];
[app scheduleLocalNotification:notification]; } }
删除一条推送
void PushNotificationIOS::removeNoticfy(std::string key)
{
// 获得 UIApplication
UIApplication *app = [UIApplication sharedApplication];
app.applicationIconBadgeNumber = ;
//获取本地推送数组
NSArray *localArray = [app scheduledLocalNotifications];
//声明本地通知对象
UILocalNotification *localNotification = nil; if (localArray)
{
for (UILocalNotification *noti in localArray)
{
NSDictionary *dict = noti.userInfo;
if (dict) {
NSString* keys = [[[NSString alloc] initWithUTF8String: key.c_str()] autorelease];
NSString* inKey = [dict objectForKey:@"DDNoticfykey"]; if ([inKey isEqualToString:keys])
{
NSLog(@"remove1 %@,%@",keys,inKey);
[app cancelLocalNotification: noti];
if (localNotification){
[localNotification release];
localNotification = nil;
}
localNotification = [noti retain];
break;
} }
} //判断是否找到已经存在的相同key的推送
if (!localNotification) {
//不存在初始化
localNotification = [[UILocalNotification alloc] init];
} if (localNotification) {
//不推送 取消推送
[app cancelLocalNotification:localNotification];
[localNotification release];
return;
}
}
}
android下没有系统直接延时本地推送的功能,我们使用AlarmManager闹钟服务,和BroadcastReceiver广播来做一个本地推送
首先建一个Cocos2dxAlarmManager类
package org.cocos2dx.lib; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log; public class Cocos2dxAlarmManager {
public static void alarmNotify(Context Context, String jsonString)
{
AlarmManager localAlarmManager = (AlarmManager)Context.getSystemService(android.content.Context.ALARM_SERVICE); String countTimeType = "rtc";
long intervalAtMillis = 86400;
long triggerAtMillis = System.currentTimeMillis() / 1000L;
int type = AlarmManager.RTC;
PendingIntent localPendingIntent;
try
{
JSONObject localJSONObject = new JSONObject(jsonString);
String packageName = localJSONObject.optString("packageName",Context.getPackageName());
String ticker = localJSONObject.optString("ticker", "null");
String title = localJSONObject.optString("title", "null");
String text = localJSONObject.optString("text", "null");
String str1 = localJSONObject.optString("tag", "noonce");
triggerAtMillis = localJSONObject.optLong("triggerAtMillis", System.currentTimeMillis() / 1000L);
long triggerOffset = localJSONObject.optLong("triggerOffset", 0L);
intervalAtMillis = localJSONObject.optLong("intervalAtMillis", 0);
countTimeType = localJSONObject.optString("countTimeType", "rtc");
triggerAtMillis *= 1000L;
long triggerOffsetMillis = triggerOffset * 1000L;
intervalAtMillis *= 1000L;
int id = localJSONObject.optInt("id", 0); if (triggerOffsetMillis > 0L)
triggerAtMillis += triggerOffsetMillis;
// if (!countTimeType.equals("rtc"))
// return; Intent localIntent = new Intent("game_receiver");//广播名,时间到了就会发送game_receiver
Bundle localBundle = new Bundle();
localBundle.putInt("flag", id);
localBundle.putString("packageName", packageName);
localBundle.putString("ticker", ticker);
localBundle.putString("title", title);
localBundle.putString("text", text);
localIntent.putExtras(localBundle);
localPendingIntent = PendingIntent.getBroadcast(Context, id, localIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if (!str1.equals("once"))
{
localAlarmManager.set(type, triggerAtMillis, localPendingIntent);
}
else
{
localAlarmManager.setRepeating(type , triggerAtMillis, intervalAtMillis, localPendingIntent);
} // Intent localIntent1 = new Intent("game_receiver");
// PendingIntent localPendingIntent1 = PendingIntent.getBroadcast(Context, 0, localIntent, 0);
long sss = System.currentTimeMillis();
sss += 10000;
Log.v("MyService","Cocos2dxAlarmManager "+(System.currentTimeMillis()-triggerAtMillis)); // localAlarmManager.set(AlarmManager.RTC_WAKEUP , triggerAtMillis, localPendingIntent);
// localAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP , System.currentTimeMillis(), 5000, localPendingIntent);
}
catch (JSONException localJSONException)
{
// localJSONException.printStackTrace();
//
// if (countTimeType.equals("rtc_wakeup"))
// type = AlarmManager.RTC_WAKEUP;
// if (countTimeType.equals("elapsed_wakeup"))
// type = AlarmManager.ELAPSED_REALTIME_WAKEUP;
// type = AlarmManager.ELAPSED_REALTIME;
//
// localAlarmManager.setRepeating(type, triggerAtMillis, intervalAtMillis, localPendingIntent);
}
} public static void cancelNotify(Context paramContext, int paramInt)
{
NotificationManager localNotificationManager = (NotificationManager)paramContext.getSystemService("notification");
localNotificationManager.cancel(paramInt); AlarmManager localAlarmManager = (AlarmManager)paramContext.getSystemService(android.content.Context.ALARM_SERVICE);
PendingIntent localPendingIntent = PendingIntent.getBroadcast(paramContext, paramInt, new Intent("game_receiver"), PendingIntent.FLAG_NO_CREATE);
if (localPendingIntent == null)
return;
localAlarmManager.cancel(localPendingIntent);
} public static void cancelNotify(Context paramContext, String paramString)
{
AlarmManager localAlarmManager = (AlarmManager)paramContext.getSystemService(android.content.Context.ALARM_SERVICE);
try
{
JSONArray localJSONArray = new JSONObject(paramString).optJSONArray("piids");
int i = 0;
if (i >= localJSONArray.length())
return;
PendingIntent localPendingIntent = PendingIntent.getBroadcast(paramContext, localJSONArray.getInt(i), new Intent("game_receiver"), PendingIntent.FLAG_NO_CREATE);
if (localPendingIntent != null)
localAlarmManager.cancel(localPendingIntent);
++i;
}
catch (JSONException localJSONException)
{
localJSONException.printStackTrace();
}
}
}
在Cocos2dxActivity.java中添加一个方法给调用者使用
public static void addNoticfy(String title,String content,int delalt,int key,int repeatTime)
{
JSONObject j = new JSONObject();
try {
j.put("ticker", content);
j.put("title", title);
j.put("text", content);
if(repeatTime<=0)
{
j.put("tag", "once");
}
else
{
j.put("intervalAtMillis", repeatTime);
}
j.put("triggerOffset", delalt);
j.put("id", key);
j.put("packageName", "com.xxx.cxxxx");//包名注意填
Cocos2dxAlarmManager.alarmNotify(instance, j.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
再添加一个Cocos2dxBroadcastReceiver类用于接收广播
package org.cocos2dx.lib; import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log; public class Cocos2dxBroadcastReceiver extends BroadcastReceiver
{ @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub if(intent.getAction().equals("game_receiver"))
{
Log.v("MyService","Cocos2dxPushService onReceive");
Bundle localBundle = intent.getExtras();
int flag = localBundle.getInt("flag");
String packageName = localBundle.getString("packageName");
String ticker = localBundle.getString("ticker");
String title = localBundle.getString("title");
String text = localBundle.getString("text");
int id = localBundle.getInt("id");
Log.v("MyService","Cocos2dxPushService onReceive2 "+packageName);
Cocos2dxNotification.doNotify(context, packageName, ticker, title, text,id);//开始本地推送
}
}
}
再添加一个推送消息类
package org.cocos2dx.lib; import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log; public class Cocos2dxNotification {
public static void doNotify(Context paramContext, String packageName, String ticker, String title, String text, int id)
{int icon = paramContext.getResources().getIdentifier("notification_icon", "drawable", paramContext.getPackageName()); NotificationManager localNotificationManager = (NotificationManager)paramContext.getSystemService("notification");
NotificationCompat.Builder localBuilder = new NotificationCompat.Builder(paramContext);
localBuilder.setSmallIcon(icon);
localBuilder.setTicker(ticker);
localBuilder.setContentTitle(title);
localBuilder.setContentText(text);
localBuilder.setAutoCancel(true);
try
{
Log.v("MyService",packageName);
Log.v("MyService",Class.forName(packageName).toString());
Intent localIntent = new Intent(paramContext, Class.forName(packageName));
localIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
localIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
localBuilder.setContentIntent(PendingIntent.getActivity(paramContext, 0, localIntent, PendingIntent.FLAG_ONE_SHOT));
Notification notfi =localBuilder.build();
notfi.defaults=Notification.DEFAULT_SOUND;
notfi.defaults |= Notification.DEFAULT_VIBRATE;
notfi.defaults|=Notification.DEFAULT_LIGHTS;
localNotificationManager.notify(id, notfi);
return;
}
catch (ClassNotFoundException localClassNotFoundException)
{
localClassNotFoundException.printStackTrace();
}
}
}
使用时只要使用Cocos2dxActivity.java中
public static void addNoticfy(String title,String content,int delalt,int key,int repeatTime)
方法就可以和IOS一样了。
cocos2d-x中本地推送消息的更多相关文章
- iOS -- 处理推送消息
简介 很多应用都会实现推送功能,我们可以集成第三方框架实现推送功能,比如: JPush推送:https://www.jpush.cn 个推:http://www.getui.com 下面来说说收到推送 ...
- iOS8推送消息的回复处理速度
iOS8我们有一个新的通知中心,我们有一个新的通报机制.当在屏幕的顶部仅需要接收一个推拉向下,你可以看到高速接口,天赋并不需要输入应用程序的操作.锁定屏幕,用于高速处理可以推动项目. 推送信息,再次提 ...
- iOS8推送消息的快速回复处理
http://blog.csdn.net/yujianxiang666/article/details/35260135 iOS8拥有了全新的通知中心,有全新的通知机制.当屏幕顶部收到推送时只需要往下 ...
- 本地推送UILocalNotification(转)
1.增加一个本地推送 //设置20秒之后 NSDate *date = [NSDate dateWithTimeIntervalSinceNow:]; //chuagjian一个本地推送 UILoca ...
- IOS中程序如何进行推送消息(本地推送,远程推送)
[1]-------------什么是推送消息? 我就以一张图解释------------ [2]-----------IOS程序中如何进行本地推送?----------- 2.1,先征求用户同意 1 ...
- IOS中程序如何进行推送消息(本地推送,远程推送)2(上)
未看过本地推送的,可以提前看一下本地推送. http://www.cnblogs.com/wolfhous/p/5135711.html =============================== ...
- IOS 本地通知推送消息
在现在的移动设备中,好多应用性的APP都用到了推送服务,但是有好多推送的内容,比如有的只是单纯的进行推送一个闹钟类型的,起了提醒作 用,有的则是推送的实质性的内容,这就分为推送的内容来区别用什么推送, ...
- SWIFT推送之本地推送(UILocalNotification)之二带按钮的消息
上一篇讲到的本地推送是普通的消息推送,本篇要讲一下带按钮动作的推送消息,先上个图瞅瞅: 继上一篇的内容进行小小的改动: 在didFinishLaunchingWithOptions方法内进行以下修改 ...
- iOS-推送,证书申请,本地推送
介绍一点点背景资料 众所周知,使用推送通知是一个很棒的.给应用添加实时消息通知的方式.这样做的结局是,开发者和用户之间,彼此永远保持着一种令人愉悦的亲密关系. 然而不幸的是,iOS的推送通知并非那么容 ...
随机推荐
- How does it work in C#? - Part 3 (C# LINQ in detail)
http://www.codeproject.com/Articles/383749/How-does-it-work-in-Csharp-Part-Csharp-LINQ-in-d
- JS 动态显示 获取下拉框的多个值
<script type="text/javascript"> function GetProcessVal(i, t) { document.getElementsB ...
- Ajax时代 SQL注入依然是隐患
许多网站程序在编写时,没有对用户输入数据的合法性进行判断,使应用程序存在安全隐患.用户可以提交一段数据库查询代码(一般是在浏览器地址栏进行,通过正常的www端口访问),根据程序返回的结果,获得某些想得 ...
- apache开源项目--TIKA
Tika是一个内容抽取的工具集合(a toolkit for text extracting).它集成了POI, Pdfbox 并且为文本抽取工作提供了一个统一的界面.其次,Tika也提供了便利的扩展 ...
- 15个极好的Linux find命令示例(二)
前阵子,我们审查了15件实事 find命令的例子(第一部分).查找命令可以做很多比只是在寻找基于名称的文件 (第2部分)在这篇文章中,让我们来讨论15高级find命令的例子, 包括-根据它访问,修改或 ...
- 多线程程序设计学习(10)Future pattern
Future pattern[订单取货模式] 一:Future pattern的参与者--->Client(客户需求)--->Host(蛋糕门店)--->Data(票据和蛋糕的接口) ...
- JS组件Bootstrap实现弹出框和提示框效果代码
这篇文章主要介绍了JS组件Bootstrap实现弹出框和提示框效果代码,对弹出框和提示框感兴趣的小伙伴们可以参考一下 前言:对于Web开发人员,弹出框和提示框的使用肯定不会陌生,比如常见的表格新增和编 ...
- Timus 1746 Hyperrook
题意:在一个n维坐标系中,坐标的范围是0到m - 1,如果两个点坐标只有一个维度的坐标不同则可以相互移动,给出p个点,问任意两个点之间路径为d的个数是多少,答案与p取模. 解法:只需要考虑两个点之间不 ...
- WebService的发布及客户端的调用
一.目录 1.JAX-WS发布WebService 1.1 创建一个简单的WS 1.2 打包部署和发布 2.CXF+Spring发布WebService 3.客户端的调用方式 二.正文 1. JAX- ...
- js基础第一天
js作用:网页特效(电梯导航).交互.表单特效.就是可以用来控制结构和样式. 常用的三个输出语句都属于js的内置对象,提供我们直接使用的功能就是内置对象功能. web三标准:结构.样式.行为.而js主 ...