【原】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的推送通知并非那么容 ...
随机推荐
- PHP会话Session
<?php //开启会话,PHP会话也提供多种存储方式,文件.数据库等 session_start(); if(isset($_GET['user'])) { $_SESSION['user'] ...
- C语言 线性表 链式表结构 实现
一个单链式实现的线性表 mList (GCC编译). /** * @brief 线性表的链式实现 (单链表) * @author wid * @date 2013-10-21 * * @note 若代 ...
- Kali Linux Web 渗透测试视频教程—第十课 w3af
Kali Linux Web 渗透测试视频教程—第十课 w3af 文/玄魂 原文链接:http://www.xuanhun521.com/Blog/2014/10/24/kali-linux-web- ...
- 用命令提示符压缩文件,解压缩文件(不需要客户端安装7zip)
压缩成一个CAB包的办法: type list.txt (生成一个文件列表) makecab /f list.txt /d compressiontype=mszip /d compressionme ...
- .Net免费公开课视频+资料+源码+经典牛逼 汇总篇【持续更新】
博主推荐一:WP8.1最经典培训教程 博主点评:经典Windows Phone8.1 Runtime API培训最经典教程,此教程由传智播客蒋坤老师录制的一整套WP8.1入门级视频教程,讲授内容非常广 ...
- [游戏模版5] Win32 折线 弧线
>_<:first build some points put in poly1[],poly2[] and poly3[] in the function of InitInstance ...
- lucene join解决父子关系索引
http://www.cnblogs.com/LBSer/p/4417074.html 1 背景 以商家(Poi)维度来展示各种服务(比如团购(deal).直连)正变得越来越流行(图1a), 比如目前 ...
- paip.多维理念 输入法的外码输入理论跟文字输出类型精髓
paip.多维理念 输入法的外码输入理论跟文字输出类型精髓 通常,我们的输入法使用的外码是拼音,但是,这个的用户体验很差.. 应该使用多个外码类型... ##按照词汇来源,有如下几个 固有词ati 来 ...
- paip.解决问题Unable to access jarfile E:\resin-4.0.22\lib\resin.jar
paip.解决问题Unable to access jarfile E:\resin-4.0.22\lib\resin.jar 作者Attilax 艾龙, EMAIL:1466519819@qq. ...
- (转)数据库获得当前时间getdate()
CONVERT(nvarchar(10),count_time,121): CONVERT为日期转换函数,一般就是在时间类型 (datetime,smalldatetime)与字符串类型(nchar, ...