当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS消息远程推送通知

iOS消息远程推送通知

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例为大家分享了ios消息推送、ios远程通知代码,供大家参考,具体内容如下

消息推送

/*
 要开发测试消息机制的程序,必须用真机测试
 
 推送消息的类型
 uiremotenotificationtypenone  不接收推送消息
 uiremotenotificationtypebadge  接收图标数字
 uiremotenotificationtypesound  接收音频
 uiremotenotificationtypealert  接收消息文字
 uiremotenotificationtypenewsstandcontentavailability 接收订阅消息
 
 要想监听到注册的devicetoken需要在苹果的开发者中心,进行一些设置工作才可以。
 */

- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
{
  // 设置应用程序能够接收apns推送的消息
  [application registerforremotenotificationtypes:uiremotenotificationtypealert | uiremotenotificationtypebadge | uiremotenotificationtypesound];
  
  self.window = [[uiwindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];
  self.window.backgroundcolor = [uicolor whitecolor];
  [self.window makekeyandvisible];
  
  return yes;
}

#pragma mark - 获取devicetoken
- (void)application:(uiapplication *)application didregisterforremotenotificationswithdevicetoken:(nsdata *)devicetoken
{
  nslog(@"%@", devicetoken);
  // 1. 从系统偏好取之前的token
  nsdata *oldtoken = [[nsuserdefaults standarduserdefaults]objectforkey:@"devicetoken"];
  // 2. 新旧token进行比较
  if (![oldtoken isequaltodata:devicetoken]) {
    // 3. 如果不一致,保存token到系统偏好
    [[nsuserdefaults standarduserdefaults]setobject:devicetoken forkey:@"devicetoken"];
    
    // 4. 使用post请求传输新旧token至服务器
    // 1) url
    // 具体的url地址以及post请求中的参数和格式,是由公司的后端程序员提供的
    // 2) request post body(包含新旧token的数据)
    // 3) connection 的异步
  }
}

远程通知

/**
 远程消息推送必须在真机上运行!
 */
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
{
  // 需要告诉苹果的服务器,当前应用程序需要接收远程通知
  [application registerforremotenotificationtypes:uiremotenotificationtypealert | uiremotenotificationtypebadge | uiremotenotificationtypesound];
  
  return yes;
}

#pragma mark - 获取到设备的代号(令牌)
// 接收到苹果返回的设备代号
- (void)application:(uiapplication *)application didregisterforremotenotificationswithdevicetoken:(nsdata *)devicetoken
{
  // 第一次运行获取到devicetoken时间会比较长!
  nslog(@"%@", devicetoken);
  
  // 将devicetoken转换成字符串,以便后续使用
  nsstring *token = [devicetoken description];
  nslog(@"description %@", token);
  
  // =======================================================
  // 如果devicetoken发生变化,需要通知服务器
  // 每次都记录住从服务器获取到得devicetoken
  // 再次获取时进行比对
  // 从偏好设置取出当前保存的token
  nsstring *oldtoken = [[nsuserdefaults standarduserdefaults] objectforkey:@"devicetoken"];
  
  // 当token发生变化时,提交给服务器保存新的token
//  if (![oldtoken isequaltostring:token]) {
//    
//    // 将devicetoken通过post请求,提交给自己的服务器即可!
//    // 发送post请求
//    nsurl *url = [nsurl urlwithstring:@"公司后台服务器的网址"];
//    nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:10.f];
//    
//    request.httpmethod = @"post";
//    request.httpbody = @"转换后的设备id以及其他信息[之前的token]";
//    
//    // sql: update t_devicetable set token = newtoken where token = oldtoken;
//    
//    // 同步:必须执行完才能继续
//    // 异步:直接交给其他线程工作,不干扰主线程工作,用户也感觉不到延迟
//    [nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue mainqueue] completionhandler:^(nsurlresponse *response, nsdata *data, nserror *connectionerror) {
//      // 偷偷的将用户信息传送到公司的服务器
//    }];
//  }
  
  // 将token保存至系统偏好
  [[nsuserdefaults standarduserdefaults] setobject:token forkey:@"devicetoken"];
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网