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>