????正在寫的App中一個本地推送需求,工作日時每天定時推送提醒,iOS端寫完已經(jīng)許久,至此才磕磕絆絆寫出Android端的實現(xiàn),特記錄于此.
? ? 基于JPush,沒有集成JPush的就不用往下看了...
Android:
/**
* 添加一條本地推送
*@parammContext上下文
*@paramtimeStr提醒時間 HH:mm
*@paramtitle推送標(biāo)題
*@paramcontent推送內(nèi)容
*@paramweekStr工作日字符串 格式:0000011 0:工作 1:不工作
*/
public static voidaddPush(Context mContext,String timeStr,String title,String content,String weekStr)
{
if(timeStr ==null|| timeStr.equals(""))
{
Log.e("LocalNotificationUtil","timeStr為空,無法設(shè)置本地推送");
return;
}
SimpleDateFormat dateFormat1 =newSimpleDateFormat("HH:mm");
SimpleDateFormat dateFormat2 =newSimpleDateFormat("yyyyMMdd");
SimpleDateFormat dateFormat3 =newSimpleDateFormat("yyyyMMdd HH:mm:ss");
Date nowDate =newDate();
//根據(jù)傳入的時間計算出
//獲取今天星期幾
String week = WeekUtil.getWeekFormDate(nowDate);
//獲取今天的推送時間
String dayStr = dateFormat2.format(nowDate);
//拼接
String notifyDateStr = dayStr+" "+timeStr +":00";
Date notifyDate? =null;
try{
notifyDate = dateFormat3.parse(notifyDateStr);
}catch(ParseException e) {
e.printStackTrace();
}
if(notifyDate ==null)
{
return;
}
intindex =0;
if(week.equals("星期一"))
{
index =0;
}
if(week.equals("星期二"))
{
index =1;
}
if(week.equals("星期三"))
{
index =2;
}
if(week.equals("星期四"))
{
index =3;
}
if(week.equals("星期五"))
{
index =4;
}
if(week.equals("星期六"))
{
index =5;
}
if(week.equals("星期天"))
{
index =6;
}
//對工作日按當(dāng)前星期幾進(jìn)行重新排序
List weekList =newArrayList<>();
for(inti = index;i < weekStr.length();i++) {
String isWork = weekStr.substring(i,i+1);
weekList.add(isWork);
}
for(inti =0;i < index;i++) {
String isWork = weekStr.substring(i,i+1);
weekList.add(isWork);
}
intoneDayTime =1000*24*60*60;
JPushLocalNotification ln =newJPushLocalNotification();
ln.setBuilderId(0);
ln.setContent(content);
ln.setTitle(title);
Map map =newHashMap();
map.put("name","jpush");
map.put("test","111");
JSONObject json =newJSONObject(map);
ln.setExtras(json.toString());
for(inti =0;i < weekList.size();i++) {
if(weekList.get(i).equals("0"))
{
//上班--設(shè)置推送
Date trueNotifyDate =newDate(notifyDate.getTime()+oneDayTime*i);
//早于當(dāng)前時間的推送,不注冊
if(trueNotifyDate.after(nowDate))
{
ln.setBroadcastTime(trueNotifyDate);
//需要為每個推送設(shè)置不同的ID,否則推送只有第一條會生效
ln.setNotificationId(trueNotifyDate.getTime());
JPushInterface.addLocalNotification(mContext,ln);
}
}
}
}
iOS:
/**
//注冊本地通知
@param IdString id
@param contentString 內(nèi)容
@param dateString 日期
@param workDaysArray 工作日
*/
+ (void)setLocalNotificationsWithId:(NSString*)IdString content:(NSString*)contentString date:(NSString*)dateString workDaysArray:(NSArray*)workDaysArray
{
//根據(jù)id取消原本的通知
[selfcancelLocalNotificationWithId:IdString];
//本地通知
JPushNotificationRequest*request = [[JPushNotificationRequestalloc]init];
//內(nèi)容
JPushNotificationContent*content = [[JPushNotificationContentalloc]init];
//content.badge = [NSNumber numberWithInt:-1];
//觸發(fā)方式
JPushNotificationTrigger*trigger = [[JPushNotificationTriggeralloc]init];
trigger.repeat=YES;
NSDateComponents*components = [[NSDateComponentsalloc]init];
//注冊或更新推送成功回調(diào)
request.completionHandler= ^(idresult) {
if(result) {
//NSLog(@"本地通知注冊成功");
}else
{
NSLog(@"本地通知注冊失敗");
}
};
for(intj =0; j < workDaysArray.count; j++) {
//設(shè)置通知內(nèi)容
content.body= contentString;
request.content= content;
//設(shè)置通知時間
NSString*hourString = [dateStringcomponentsSeparatedByString:@":"][0];
NSString*minuteString = [dateStringcomponentsSeparatedByString:@":"][1];
components.hour= [hourStringintegerValue];
components.minute= [minuteStringintegerValue];
request.requestIdentifier= [NSStringstringWithFormat:@"%@_%d",IdString,j];
components.weekday= [selfparserWeekStringToInt:workDaysArray[j]];
trigger.dateComponents= components;
request.trigger= trigger;
[JPUSHServiceaddNotification:request];
}
}