项目介绍: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分钟以内的误差,可能是基于省电的考虑。