欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ios 消息推送详情

程序员文章站 2022-10-26 14:38:49
消息推送流程 手机会一直保持与苹果apns一个通信 当打开app时候,会从苹果apns得到一个token 发送token到后台服务器 服务器发送这个token 和 推送数据给苹果apns (以为pu...

消息推送流程

手机会一直保持与苹果apns一个通信

当打开app时候,会从苹果apns得到一个token

发送token到后台服务器

服务器发送这个token 和 推送数据给苹果apns (以为push通道是由苹果维护的一个唯一的通道)

苹果apns得到后再根据这个token推送得到的消息给我们的ios设备

我们得到token后就知道是哪个app的推送内容了

ios 消息推送详情

ios 消息推送详情

消息推送(以v8.0+为例子)(搭建push环境)

- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions

{

//读取系统版本,判断是否大于8.0

if([[[uidevice currentdevice] systemversion] floatvalue] >=8.0) {

//首先向用户询问push的权限,向用户申请权限

uiusernotificationsettings* setting = [uiusernotificationsettings settingsfortypes:uiusernotificationtypebadge | uiusernotificationtypesound | uiusernotificationtypealert categories:nil];

[[uiapplication sharedapplication] registerusernotificationsettings:setting];

}else{

[[uiapplication sharedapplication] registerforremotenotificationtypes:(uiremotenotificationtypebadge | uiremotenotificationtypesound | uiremotenotificationtypealert)];

}

}

//用户同意之后回调,去注册devicetoken

- (void)application:(uiapplication *)application didregisterusernotificationsettings:(nonnull uiusernotificationsettings *)notificationsettings{

[[uiapplication sharedapplication] registerforremotenotifications];

}

//注册成功回调,得到devicetoken

- (void)application:(uiapplication *)application didregisterforremotenotificationswithdevicetoken:(nsdata *)adevicetoken {

dlog(@“succeed regist remote notification");

nsstring* tokenstr = [nsstring stringwithformat:@"%@", adevicetoken];

tokenstr = [tokenstr stringbyreplacingoccurrencesofstring:@" " withstring:@""];

tokenstr = [tokenstr stringbyreplacingoccurrencesofstring:@"<" withstring:@""];

tokenstr = [tokenstr stringbyreplacingoccurrencesofstring:@">" withstring:@""];

//向服务器发送

}

//注册失败回调

- (void)application:(uiapplication *)application didfailtoregisterforremotenotificationswitherror:(nserror *)error {

dlog(@"failed regist remote notification %@ ", error);

}

//当接收到远程通知回调

- (void)application:(uiapplication *)application didreceiveremotenotification:(nsdictionary *)userinfo {

nsdictionary *apsdictionary = [userinfo objectforkey:@"aps"];

//解析json

/*

具体处理。。。

if(...){

跳转某个controller。。。

}

*/

}

消息推送(本地推送)

@property (nonatomic, retain) uilocalnotification *localnotification;

- (void)background{

_localnotification = [uilocalnotification new];

//多久以后提示用户

_localnotification.firedate = [nsdate datewithtimeintervalsincenow:5];

_localnotification.alertbody = @"你都走了5秒钟了";

_localnotification.alerttitle = @"你妹的!";

_localnotification.userinfo = @{@"key":@"value"};

[[uiapplication sharedapplication] schedulelocalnotification:_localnotification];

}

- (void)foreground{

[[uiapplication sharedapplication] cancellocalnotification:_localnotification];

}

- (void)viewdidload

{

[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(background) name:uiapplicationdidenterbackgroundnotification object:nil];

[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(foreground) name:uiapplicationwillenterforegroundnotification object:nil];

}