【原】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的推送通知并非那么容 ...
随机推荐
- 获取root权限
1.用root建立一个普通用户mary,并切换到mary. < 2.我们首先测试一下当前用户的权限 3.进入到/tmp,新建目录abc. 4.执行下列相关命令.并保证最后一行后面的两块红色部分为 ...
- AS3从入门到放弃
工作久了,在技术上肯定有自己的一些见解.一直以来都懒得写下来,总觉得尤其写博客的时间,还不如自己学一点新东西.但不能总找这样的接口啊,于是乎开始了这篇博客. 工作了三年,有一年半的时间是在做AS3,在 ...
- 将枚举定义生成SQL中的Case-When-then语句
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- Makeflow 4.0 发布,工作流引擎
Makeflow 4.0 发布了,主要改进包括: 1. 支持分层次的 workers,带 master-foremen-workers 范式. 2. 一个 worker 可同时处理超过 1 个的任务3 ...
- 深入理解java虚拟机【内存溢出实例】
通过简单的小例子程序,演示java虚拟机各部分内存溢出情况: (1).java堆溢出: Java堆用于存储实例对象,只要不断创建对象,并且保证GC Roots到对象之间有引用的可达,避免垃圾收集器回收 ...
- [.NET 即时通信SignalR] 认识SignalR (一)
ASP .NET SignalR[1] 是一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信.什么是实时通信的Web呢?就是让客户端(Web页面)和服务器端可以互相通知 ...
- 解读SQL Server 2014可更新列存储索引——存储机制
概述 SQL Server 2014被号称是微软数据库的一个革命性版本,其性能的提升的幅度是有史以来之最. 可更新的列存储索引作为SQL Server 2014的一个关键功能之一,在提升数据库的查询性 ...
- JS 基本数据类型
一.undefined 类型 (ECMAScript 3引入undefined类型) 1.它的值只有一个 undefined 2.未初始化的变量 会隐式转换为undeFined类型 var box; ...
- python redis使用
#!/usr/bin/python #coding=utf-8 import redis class CRedis: def __init__(self): self.host = 'localhos ...
- Android布局优化
前言 本篇文章为Android优化的布局部分,该部分应该是Android中很重要的,无论是在自定义控件中,还是在简单的书写布局时,都应该尽量遵循一些优化原则,这样布局的绘制效率才会更高,体验才能更好. ...