美文网首页
基于JPush的本地推送(iOS/Android)

基于JPush的本地推送(iOS/Android)

作者: OrrHsiao | 来源:发表于2017-12-15 23:13 被阅读0次

    正在写的App中一个本地推送需求,工作日时每天定时推送提醒,iOS端写完已经许久,至此才磕磕绊绊写出Android端的实现,特记录于此.

    基于JPush,没有集成JPush的就不用往下看了...


Android:

/**

* 添加一条本地推送

*@parammContext上下文

*@paramtimeStr提醒时间 HH:mm

*@paramtitle推送标题

*@paramcontent推送内容

*@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为空,无法设置本地推送");

return;

}

SimpleDateFormat dateFormat1 =newSimpleDateFormat("HH:mm");

SimpleDateFormat dateFormat2 =newSimpleDateFormat("yyyyMMdd");

SimpleDateFormat dateFormat3 =newSimpleDateFormat("yyyyMMdd HH:mm:ss");

Date nowDate =newDate();

//根据传入的时间计算出

//获取今天星期几

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;

}

//对工作日按当前星期几进行重新排序

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"))

{

//上班--设置推送

Date trueNotifyDate =newDate(notifyDate.getTime()+oneDayTime*i);

//早于当前时间的推送,不注册

if(trueNotifyDate.after(nowDate))

{

ln.setBroadcastTime(trueNotifyDate);

//需要为每个推送设置不同的ID,否则推送只有第一条会生效

ln.setNotificationId(trueNotifyDate.getTime());

JPushInterface.addLocalNotification(mContext,ln);

}

}

}

}


iOS:

/**

//注册本地通知

@param IdString id

@param contentString 内容

@param dateString 日期

@param workDaysArray 工作日

*/

+ (void)setLocalNotificationsWithId:(NSString*)IdString content:(NSString*)contentString date:(NSString*)dateString workDaysArray:(NSArray*)workDaysArray

{

//根据id取消原本的通知

[selfcancelLocalNotificationWithId:IdString];

//本地通知

JPushNotificationRequest*request = [[JPushNotificationRequestalloc]init];

//内容

JPushNotificationContent*content = [[JPushNotificationContentalloc]init];

//content.badge = [NSNumber numberWithInt:-1];

//触发方式

JPushNotificationTrigger*trigger = [[JPushNotificationTriggeralloc]init];

trigger.repeat=YES;

NSDateComponents*components = [[NSDateComponentsalloc]init];

//注册或更新推送成功回调

request.completionHandler= ^(idresult) {

if(result) {

//NSLog(@"本地通知注册成功");

}else

{

NSLog(@"本地通知注册失败");

}

};

for(intj =0; j < workDaysArray.count; j++) {

//设置通知内容

content.body= contentString;

request.content= content;

//设置通知时间

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];

}

}

相关文章

网友评论

      本文标题:基于JPush的本地推送(iOS/Android)

      本文链接:https://www.haomeiwen.com/subject/qysjwxtx.html