【原】android本地推送
android本地推送的实现原理:开启一个BroadcastReceiver和一个AlarmManager,闹钟设置推送唤醒时间,BroadcastReceiver一直在检测是否应该推送。
目前遗留问题,好多手机 关闭应用 service被杀死,无法接受推送。各种重启service我也试了 小米手机就是不好使! 要是确保service不死 完美收到推送
public static String PushAction = "cn.XXX.PushAction";
pushData="1|2|09:50|内容^2|2|09:58|内容" // id|类型|时间|内容
设置重复型闹钟
SharedPreferences sharedPreferences = Cocos2dxActivity.getContext().getSharedPreferences("SP", Cocos2dxActivity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("key", pushData);
editor.commit();
Intent intent =new Intent(Cocos2dxActivity.getContext(), PushReceiver.class);
intent.setAction(PushAction);
PendingIntent sender=PendingIntent.getBroadcast(Cocos2dxActivity.getContext(), 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarm=(AlarmManager)Cocos2dxActivity.getContext().getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC,System.currentTimeMillis(),60*1000, sender); --设置每隔一分钟发送一次PushAction 设置重复执行
设置一次型闹钟
long t = Long.parseLong(time)*1000+System.currentTimeMillis();
Intent intent =new Intent(Cocos2dxActivity.getContext(), PushReceiver.class);
intent.setAction(PushAction);
intent.putExtra("id", id);--注意这个id最好唯一,假如设置多条推送时 ,id必须唯一 要不就乱了
intent.putExtra("content", body);
intent.putExtra("type",2); //对应PushReceiver 类型判断
PendingIntent sender=PendingIntent.getBroadcast(Cocos2dxActivity.getContext(), id, intent, PendingIntent.FLAG_CANCEL_CURRENT); --注意第二个参数 一定唯一 当有多条推送的时候
AlarmManager alarm=(AlarmManager)Cocos2dxActivity.getContext().getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC, t, sender);--从当前开始 间隔time之后 触发推送
触发推送的实现 PushReceiver类
@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(Push.PushAction))
{
pushNotify(arg0); // 设置重复性推送
if(intent.getIntExtra("type",0) ==2){//对应之前一次型推送里面的类型
sendNotify1(intent.getIntExtra("id",0),intent.getStringExtra("content"),arg0);
}
}
}
public static void pushNotify(Context ctx) {
SharedPreferences sharedPreferences = ctx.getSharedPreferences("SP", Cocos2dxActivity.MODE_PRIVATE);
String con = sharedPreferences.getString("key", "");
Log.e("EEEE", con);
String temp[] = con.split("\\^");
if (temp.length<=0) return;
int week =Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
int hour =Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
String strHour = "";
if (hour<=9)
{
strHour = "0"+hour;
}
else {
strHour = hour+"";
}
int mimute = Calendar.getInstance().get(Calendar.MINUTE);
String strMimute= "";
if (mimute<=9) {
strMimute ="0"+mimute;
}
else {
strMimute = mimute+"";
}
for(int i=0;i<temp.length;i++)
{
String pushStr[] = temp[i].split("\\|");
int id = Integer.parseInt(pushStr[0]) ;
int type = Integer.parseInt(pushStr[1]) ;
String time = pushStr[2];
String content = pushStr[3];
switch (type) {
case 2: //设置几点几分的推送
String t =strHour+":"+strMimute;
if (time.equals(t)){
sendNotify1(id, content,ctx);
}
break;
case 3: //星期几几点几分的推送
int tempWeek =0;
switch (week) {
case 1:
tempWeek = 7;
break;
case 2:
tempWeek = 1;
break;
case 3:
tempWeek = 2;
break;
case 4:
tempWeek = 3;
break;
case 5:
tempWeek = 4;
break;
case 6:
tempWeek = 5;
break;
case 7:
tempWeek = 6;
break;
default:
break;
}
String t1 =tempWeek+":"+strHour+":"+strMimute;
if (time.equals(t1)){
sendNotify1(id, content,ctx);
}
week = 0;
break;
default:
break;
}
}
}
@SuppressWarnings("deprecation") //设置推送
public static void sendNotify1(final int id,final String body,final Context ctx)
{
NotificationManager nm = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Notification noti = new Notification(R.drawable.icon, body,System.currentTimeMillis());
noti.defaults = Notification.DEFAULT_SOUND;
String title = ctx.getString(R.string.app_name);
noti.flags = Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(ctx, Pokemon.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, id,intent, PendingIntent.FLAG_UPDATE_CURRENT);
noti.setLatestEventInfo(ctx,title, body, contentIntent);
nm.notify(id, noti);
}
AndroidManifest.xml配置
<receiver android:name="cn.XXX.PushReceiver" >
<intent-filter android:priority = "1000" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="cn.XXX.PushAction" />
</intent-filter>
</receiver>
【原】android本地推送的更多相关文章
- [android] 本地推送服务
遇到新需求:游戏要添加本地的推送功能,ios比较好搞,在应用退出时的系统回调中设置,android就稍稍麻烦一点,需要用到 android中的service,和receiver
- unity3d 之本地推送
1. 本地推送主要包括在android和ios上,下面所有的代码都是本人写的,经过测试是没有问题的,已经运用到项目中了.首先是接口INotification: using System; public ...
- iOS本地推送与远程推送
原文在此 分为本地推送和远程推送2种.可以在应用没有打开甚至手机锁屏情况下给用户以提示.它们都需要注册,注册后系统会弹出提示框(如下图)提示用户是否同意,如果同意则正常使用:如果用户不同意则下次打开程 ...
- Android消息推送——JPush极光推送
刚看了一篇关于Android消息推送评测总结的博客http://www.cnblogs.com/logan/p/4514635.html: 自己也对原学过的JPush极光进行一下小结,方便后续工作使用 ...
- Android实现推送方式解决方案(转)
本文介绍在Android中实现推送方式的基础知识及相关解决方案.推送功能在手机开发中应用的场景是越来起来了,不说别的,就我们手机上的新闻客户端就时不j时的推送过来新的消息,很方便的阅读最新的新闻信息. ...
- cocos2d-x中本地推送消息
作者:HU 转载请注明,原文链接:http://www.cnblogs.com/xioapingguo/p/4038277.html IOS下很简单: 添加一条推送 void PushNotific ...
- iOS本地推送与远程推送详解
一.简介 分为本地推送和远程推送2种.可以在应用没有打开甚至手机锁屏情况下给用户以提示.它们都需要注册,注册后系统会弹出提示框(如下图)提示用户是否同意,如果同意则正常使用:如果用户不同意则下次打开程 ...
- 81、iOS本地推送与远程推送详解
一.简介 分为本地推送和远程推送2种.可以在应用没打开甚至手机锁屏情况下给用户以提示.它们都需要注册,注册后系统会弹出提示框(如下图)提示用户石否同意,如果同意则正常使用:如果用户不同意则下次打开程序 ...
- iOS-推送,证书申请,本地推送
介绍一点点背景资料 众所周知,使用推送通知是一个很棒的.给应用添加实时消息通知的方式.这样做的结局是,开发者和用户之间,彼此永远保持着一种令人愉悦的亲密关系. 然而不幸的是,iOS的推送通知并非那么容 ...
随机推荐
- C#函数式编程之惰性求值
惰性求值 在开始介绍今天要讲的知识之前,我们想要理解严格求值策略和非严格求值策略之间的区别,这样我们才能够深有体会的明白为什么需要利用这个技术.首先需要说明的是C#语言小部分采用了非严格求值策略,大部 ...
- 安卓系统上安装.net运行时 mono runtime
感谢以下博主: ubuntu指南 http://dawndiy.com/archives/229/ img大小调整 http://zebinj.blog.163.com/blog/static/206 ...
- [游戏模版19] Win32 物理引擎 匀速运动
>_<:Learning the physical engine >_<:resource >_<:code #include <windows.h> ...
- AngularJS快速入门指南11:事件
AngularJS拥有自己的HTML事件指令. ng-click指令 ng-click指令定义了AngularJS的click事件. <div ng-app="" ng-co ...
- [BTS] WCF-OracleDB
When I insert some data to Oracle, BizTalk WCF-OracleDB throw this error. A message sent to adapter ...
- jQuery Ztree基本用法
1.首先在页面上有<ul/>标签 <ul id="tree" class="ztree"></ul> 2.定义ztree的配 ...
- 利用nodejs模块缓存机制创建“全局变量”
在<深入浅出nodejs>有这样一段(有部分增减): 1.nodejs引入模块分四个步骤 路径分析 文件定位 编译执行 加入内存 2.核心模块部分在node源代码的编译过程中就编译成了二级 ...
- GCD中的dispatch_sync、dispatch_sync 分别与串行、并行队列组合执行小实验
平常开发中会经常用gcd做一下多线程任务,但一直没有对同步.异步任务在串行.并行队列的执行情况做个全面的认识,今天写了个demo跑了下,还是有些新发现的. 代码如下: - (void)touchesB ...
- atitit.404错误的排查流程总结vOa6
atitit.404错误的排查流程总结vOa6 1. 场景 1 1.1. 子应用猛个腊擦不能使用 404 兰.. 1 2. 服务器配置问题 2 2.1. 登录服务器管理子应用,查看应用是否启动okk ...
- paip.提升用户体验--提升java的热部署热更新能力
paip.提升用户体验--提升java的热部署热更新能力 想让java做到php那么好的热部署能力 "fix online"/在线修复吗??直接在服务器上修改源码生效,无需重启应 ...