【原】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的推送通知并非那么容 ...
随机推荐
- QT中QProcess调用命令行的痛苦经历
在QT程序中需要将某些目录和文件压缩为一个rar的压缩包,于是想到了在QT中通过QProcess类调用命令行的rar.exe来达到效果,但是没想到QProcess类用起来很麻烦,而且达不到效果,折腾了 ...
- 跟我一起学WCF(2)——利用.NET Remoting技术开发分布式应用
一.引言 上一篇博文分享了消息队列(MSMQ)技术来实现分布式应用,在这篇博文继续分享下.NET平台下另一种分布式技术——.NET Remoting. 二..NET Remoting 介绍 2.1 . ...
- 为什么在Mac中无法用k web运行ASP.NET 5程序
k web对应的命令如下: "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebLi ...
- 高手速成android开源项目【blog篇】
主要介绍那些乐于分享并且有一些很不错的开源项目的个人和组织.Follow大神,深挖大神的项目和following,你会发现很多. 一.个人 JakeWharton 就职于SquareGithub地址: ...
- Nginx学习笔记(六) 源码分析&启动过程
Nginx的启动过程 主要介绍Nginx的启动过程,可以在/core/nginx.c中找到Nginx的主函数main(),那么就从这里开始分析Nginx的启动过程. 涉及到的基本函数 源码: /* * ...
- ichat在线客服jQuery插件(可能是历史上最灵活的)
ichat是一款开源免费在线客服jQuery插件,通过该插件,您可以自由的定制属于自己的在线客服代码. ichat充分吸收传统在线客服插件的优点,并加上自身的独特设计,使得ichat可定制性异常强大. ...
- jenkins2 pipeline实例
比较完整的实例,使用了maven打包,git tag,发通知邮件,等待用户deploy,deploy到nexus. 文章来自:http://www.ciandcd.com文中的代码来自可以从githu ...
- HTML Meta标签知多少
文章已同步至个人Blog:Benjamin - 专注前端开发和用户体验 一.基本属性 标签常常被用来定义HTML文档的元数据或者HTTP协议的指向,这些元数据常用在SEO.HTML Pages or ...
- CSS行高——line-height
初入前端的时候觉得CSS知道display.position.float就可以在布局上游刃有余了,随着以后工作问题层出不穷,才逐渐了解到CSS并不是几个style属性那么简单,最近看了一些关于行高的知 ...
- HTML标签简明学习一
!-- ... -- html注释 浏览器不对其中的内容解析,可以用来调试及书写释意 <!-- 动不动就被注释 --> !DOCTYPE 声明文件类型 一般大写,必须位于文档首行,浏览器根 ...