【原】android本地推送

來源:互聯網
上載者:User

標籤:

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本地推送

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.