Android本地消息推送
项目介绍:cocos2dx跨平台游戏
项目需求:实现本地消息推送,需求①:定点推送;需求②:根据游戏内逻辑实现推送(比如玩家体力满时,需要计算后到点推送);需求③:清理后台程序或重启后依然能够实现本地推送。
功能实现:由于IOS有一套比较成熟的UILocalNotification推送机制,这里主要说明Android下本地推送的实现。另外大家感兴趣可以看下第三方的推送:个推、极光、腾讯信鸽、百度云推送等,第三方多是要接入服务端,否则只能自己在第三方申请的应用的后台手动推送,另外第三方也不保证能100%所有客户端都能接收到推送。自己游戏里接入了信鸽,亲试,开启游戏可以收到推送,关闭游戏未能收到而是在再次启动游戏时收到。看来接收也有待优化。
1.全局定时器AlarmManager,可参考闹钟app,AlarmManager为系统级别,所以一般不会被清理掉,并把设定的提醒保存到本地(这里使用的SharedPreference,也可使用SQLite数据库存储),开机重启时重新设置定时提醒。
/**
* 消息推送
* noticeStr:通知内容
* tiemstamp:通知的启动的时间戳,单位为秒,定时器单位为毫秒
*/
public int noticeCount = 0;
public void pushMessage(String noticeStr, long timestamp) {
//System.currentTimeMillis() 等于 Calendar.getInstance().getTimeInMillis()
long longTime = timestamp*1000;if (longTime > System.currentTimeMillis()) {
Intent intent = new Intent(this, PushReceiver.class);
//设置参数
intent.putExtra("noticeId", noticeCount);
intent.putExtra("noticeStr", noticeStr);
//timestamp参数 区别要注册的PendingIntent
//receiver获取参数需要flag设置为PendingIntent.FLAG_UPDATE_CURRENT
PendingIntent pi = PendingIntent.getBroadcast(FunmAndroid.this, noticeCount, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);;
am.set(AlarmManager.RTC_WAKEUP, longTime, pi);
//本地存储,手机重启,需要重新设置
SharedPreferences sharedPreferences = getSharedPreferences("funm_push", Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putLong("tiemstamp_"+noticeCount, longTime);
editor.putString("noticeStr_"+noticeCount, noticeStr);
editor.putInt("noticeCount", noticeCount);
Log.v("and_log", "put noticeCount: "+noticeCount);
editor.commit();
noticeCount++;
}
}
2.接收广播:BroadCastReceiver,注意这里使用BroadCastReceiver,不要使用service。开机重新设置提醒。
package com.funcity.funm.push; import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences; import com.funcity.funm.FunmAndroid; public class BootReceiver extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent1) {
// TODO Auto-generated method stub
String action = intent1.getAction();
if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
resetPush(context);
}
} private void resetPush(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences("funm_push", Context.MODE_PRIVATE);
int count = sharedPreferences.getInt("noticeCount", 0);
int noticeCount = 0;
for (int i=0; i<count; i++) {
long timestamp = sharedPreferences.getLong("tiemstamp_"+noticeCount, 0);
String noticeStr = sharedPreferences.getString("noticeStr_"+noticeCount, "");
if (timestamp !=0 && !noticeStr.equals("")) {
Intent playerIntent = new Intent(context, PushReceiver.class);
playerIntent.putExtra("noticeId", noticeCount);
playerIntent.putExtra("noticeStr", noticeStr);
PendingIntent pi = PendingIntent.getBroadcast(context, noticeCount, playerIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, timestamp, pi);
}
noticeCount++;
}
}
}
3.接收提醒并发起推送:
package com.funcity.funm.push; import com.fun.funm.R;
import com.funcity.funm.FunmAndroid; import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log; public class PushReceiver extends BroadcastReceiver { private NotificationManager manager;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
manager = (NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
int noticeId = intent.getIntExtra("noticeId", 0);
String noticeStr = intent.getStringExtra("noticeStr");
Intent playIntent = new Intent(context, FunmAndroid.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, playIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentTitle(FunmAndroid.getAppName()).setContentText(noticeStr).setSmallIcon(R.drawable.icon).setDefaults(Notification.DEFAULT_ALL).setContentIntent(pendingIntent).setAutoCancel(true);
manager.notify(noticeId, builder.build());
Log.v("and_log","收到推送:onReceive: "+ noticeStr);
}
}
4.Receiver注册及权限
<receiver android:name="com.funcity.funm.push.PushReceiver">
<intent-filter>
<action android:name="com.funcity.funm.push.PushReceiver"/>
</intent-filter>
</receiver>
<receiver android:name="com.funcity.funm.push.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
android:targetSdkVersion="18"
注意:
1.手机必须开启允许开启自启动权限定时推送才能在重启后依然生效。有些手机管理软件,如360会推荐关闭一些应用的开机自启动选项。
2.targetSdkVersion19以前是准时推送,貌似19之后为非准时推送,需要注意一下。
3.有些手机类型,比如小米,可能有5分钟以内的误差,可能是基于省电的考虑。
Android本地消息推送的更多相关文章
- Android开发学习笔记-关于Android的消息推送以及前后台切换
下面是最简单的Android的消息推送的实现方法 package com.example.shownotic; import java.util.Random; import android.supp ...
- Android客户端消息推送原理简介
首先简单介绍一下Android消息推送的主要三种方式,如果你已经看过类似的文章,请直接忽略三种介绍. 1.使用SMS服务,即服务器端发送短信,然后手机客户端监听短信的广播,然后对数据进行一定的处 ...
- [android] 安卓消息推送的几种实现方式
消息推送的目的:让服务器端及时的通知客户端 实现方案 轮询:客户端每隔一定的时间向服务器端发起请求,获得最新的消息 特点:如果用在最新新闻通知上,效率就有点低了,技术简单,好实现 应用场景:服务器端以 ...
- Android 生态消息推送平台介绍
一.手机厂商平台 华为消息推送服务 华为推送(Push)是为开发者提供的消息推送平台,建立了从云端到手机端的消息推送通道,使应用可以将最新信息及时通知用户,从而构筑良好的用户关系,提升用户的感知和活跃 ...
- android热门消息推送横向测评![转]
关于这个话题,已经不是什么新鲜事了.对于大多数中小型公司一般都是选择第三方的服务来实现.但是现在已经有很多提供推送服务的公司和产品,如何选择一个适合自己项目的服务呢?它们之间都有什么差别?在此为大家做 ...
- Android (Notification)消息推送机制
从网上查询资料学习Android消息推送机制,效果图如下: 1.首先是布局文件代码 activity_main.xml <?xml version="1.0" encodin ...
- Android之消息推送实现
在开发Android和iPhone应用程序时,我们往往需要从服务器不定的向手机客户端即时推送各种通知消息,iPhone上已经有了比较简单的和完美的推送通知解决方案,可是Android平台上实现起来却相 ...
- iOS/Android网络消息推送的实现两种方法
移动时代,用户为王,而每个APP拥有的活跃用户量(Active Users),决定了其价值. 消息推送成为了不可或缺的活跃唤起工具. 目前消息推送有如下两种途径: 1.iOS传统方式: 通过Apple ...
- IOS - 本地消息推送
第一步:创建本地推送 // 创建一个本地推送 UILocalNotification *notification = [[[UILocalNotification alloc] init] autor ...
随机推荐
- - > 并查集详解(第二节)
以下是并查集思路详解: 一:概念 并查集处理的是“集合"之间的关系.当给出两个元素的一个无序数对(a,b)时,需要快速“合并”a和b分别所在的集合,这期间需要反复“查找”某元素所在的集合.“ ...
- Sublime Text 3显示文本编码
在Settings上加入"show_encoding":true 进入这个选项:[Preferences]->[Settings] 搞定之后,在右下角可以看见文本编码
- Eclipse新建/导入Gradle项目
一.新建 1.[New]->[Project] 二.导入 1.[Import] 2. 参考: http://www.vogella.com/tutorials/EclipseGradle/art ...
- react 单元测试 (jest+enzyme)
为什么要做单元测试 作为一个前端工程师,我是很想去谢单元测试的,因为每天的需求很多,还要去编写测试代码,感觉时间都不够用了. 不过最近开发了一个比较复杂的项目,让我感觉一旦项目大了.复杂了,而且还是多 ...
- Oracle中的 row_number() over (partition by order by ) 用法
oracle 里面经常这样用 select col1,col2..., row_number() over (partition by colx order by coly) from table_n ...
- Unity3D Asset文件导出3DMax 可编辑格式
本文章由cartzhang编写,转载请注明出处. 全部权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/60878354 作者:car ...
- [Vue @Component] Pass Props to Vue Functional Templates
Functional templates allow you to create components consisting of only the template tag and exposing ...
- Matlab得到二值图像中最大连通区域
有时候要将二值化图像中最大的连通域保存下来.以下函数提供了一种方法: %function [img]=maxLianTongYu(I):求图像中最大的连通域 %输入:I 输入图像 %输出:img 仅包 ...
- scikit-learn: isotonic regression(保序回归,非常有意思,仅做知识点了解,但差点儿没用到过)
http://scikit-learn.org/stable/auto_examples/plot_isotonic_regression.html#example-plot-isotonic-reg ...
- Gym - 101208C 2013 ACM-ICPC World Finals C.Surely You Congest 最大流+最短路
题面 题意:给你n(2w5)个点,m条边(7w5)有k(1e3)辆车停在某些点上的,然后他们都想尽快去1号点,同时出发,同一个点不允许同时经过, 如果多辆车同时到达一个点,他们就会堵塞,这时候只能选择 ...