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本地推送的更多相关文章

  1. [android] 本地推送服务

    遇到新需求:游戏要添加本地的推送功能,ios比较好搞,在应用退出时的系统回调中设置,android就稍稍麻烦一点,需要用到 android中的service,和receiver

  2. unity3d 之本地推送

    1. 本地推送主要包括在android和ios上,下面所有的代码都是本人写的,经过测试是没有问题的,已经运用到项目中了.首先是接口INotification: using System; public ...

  3. iOS本地推送与远程推送

    原文在此 分为本地推送和远程推送2种.可以在应用没有打开甚至手机锁屏情况下给用户以提示.它们都需要注册,注册后系统会弹出提示框(如下图)提示用户是否同意,如果同意则正常使用:如果用户不同意则下次打开程 ...

  4. Android消息推送——JPush极光推送

    刚看了一篇关于Android消息推送评测总结的博客http://www.cnblogs.com/logan/p/4514635.html: 自己也对原学过的JPush极光进行一下小结,方便后续工作使用 ...

  5. Android实现推送方式解决方案(转)

    本文介绍在Android中实现推送方式的基础知识及相关解决方案.推送功能在手机开发中应用的场景是越来起来了,不说别的,就我们手机上的新闻客户端就时不j时的推送过来新的消息,很方便的阅读最新的新闻信息. ...

  6. cocos2d-x中本地推送消息

    作者:HU 转载请注明,原文链接:http://www.cnblogs.com/xioapingguo/p/4038277.html  IOS下很简单: 添加一条推送 void PushNotific ...

  7. iOS本地推送与远程推送详解

    一.简介 分为本地推送和远程推送2种.可以在应用没有打开甚至手机锁屏情况下给用户以提示.它们都需要注册,注册后系统会弹出提示框(如下图)提示用户是否同意,如果同意则正常使用:如果用户不同意则下次打开程 ...

  8. 81、iOS本地推送与远程推送详解

    一.简介 分为本地推送和远程推送2种.可以在应用没打开甚至手机锁屏情况下给用户以提示.它们都需要注册,注册后系统会弹出提示框(如下图)提示用户石否同意,如果同意则正常使用:如果用户不同意则下次打开程序 ...

  9. iOS-推送,证书申请,本地推送

    介绍一点点背景资料 众所周知,使用推送通知是一个很棒的.给应用添加实时消息通知的方式.这样做的结局是,开发者和用户之间,彼此永远保持着一种令人愉悦的亲密关系. 然而不幸的是,iOS的推送通知并非那么容 ...

随机推荐

  1. Q114第一颗二叉查找树(链式)

    输入n,然后n个树,建立二叉查找树.从小到大输出每个节点的左右子树,空输出# #include<cstdio> #include<iostream> using namespa ...

  2. C#之delegate

    delegate 委托的使用: 封装一个方法,该方法只有一个参数并且不返回值. using System; using System.Windows.Forms; delegate void Disp ...

  3. SymmetricDS 3.5.0 发布,数据同步和复制

    SymmetricDS 3.5.0 关闭 53 个问题,新增对 SQLite on Android.Sybase ASE 和 Sybase ASA 的支持:增加了文件同步功能,可同步目录.文件过滤和脚 ...

  4. [Xamarin] 透過WebClient跟網路取得資料 (转帖)

    之前寫過一篇文章,關於在Android上面取得資料 透過GET方式傳資料給Server(含解決中文編碼問題) 我們來回顧一下 Android 端的Code: 有沒有超多,如果是在Xaramin下面,真 ...

  5. jboss7(01)服务器开启和关闭命令

    1.简单开启服务器的命令:进入到 bin 目录下,输入 ./standalone.sh 命令. 这种开启服务器的方式有个缺点,当你的命令窗口关闭后,服务自动down了 2.让服务器开启后在后台运行:进 ...

  6. 微软BI 之SSRS 系列 - 巧用 RunningValue 函数在分组中排序并设置 RANK 排名

    开篇介绍 经常有像类似于这样的排序需求,以及设置分组下的排序序号.比如此图中要求城市 City 在省份下按照 Internet Sales Amount 总销售额进行排序,并标识在各省份下的排名. 实 ...

  7. [matlab] MATLAB 界面编程 傻瓜教程

    >_<:在 MATLAB 的命令窗口(Command Window)中运行 guide 命令,来打开 GUIDE 界面,如下: >_<:然后,选择空模板(Blang GUI), ...

  8. 【原】在windows下使用VirtualEnv

    VirtualEnv可以方便的解决不同项目中对类库的依赖问题.这通常是通过以下方式实现的:首先将常用的类库安装在系统环境中:然后为每个项目安装独立的类库环境.这样子可以保证每个项目都运行在独立的类库环 ...

  9. 浅谈sql中的in与not in,exists与not exists的区别

    转 浅谈sql中的in与not in,exists与not exists的区别   12月12日北京OSC源创会 —— 开源技术的年终盛典 »   sql exists in 1.in和exists ...

  10. TableView didSelectRowAtIndexPath 不执行

    1.父类事件设置代理 UIGestureRecognizer *tapGesture  ... tapGesture.delegate = self; 2.覆盖方法 - (BOOL)gestureRe ...