详解IOS开发之实现App消息推送(最新)

2020-01-18 18:31:20王旭

2.把push.p12文件生成为.pem文件

ios,app消息推送,ios消息推送,app消息推送实现

上边输入的密码则是你导出证书所设的密码,即abc123.接着还会让你输入.pem文件的密码,还是使用abc123好了,防止混淆。这样我们在push文件夹中就又得到了两个文件,PushChatCert.pem和PushChatKey.pem。

3.把PushChatCert.pem和PushChatKey.pem合并为一个pem文件,

ios,app消息推送,ios消息推送,app消息推送实现

在push文件夹中又多了一个ck.pem文件,以上我们把需要使用的文件都准备好了

接下来就要测试一下啦,是不是很激动~

为了测试证书工作的状况,我们可以使用“telnet gateway.sandbox.push.apple.com 2195”来检测一下,如果显示下图则表示成功了。

 ios,app消息推送,ios消息推送,app消息推送实现

然后,我们使用我们生成的证书和私钥来设置一个安全的链接去链接苹果服务器

在终端输入如下命令:openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushChatKey.pem

需要输入密码(abc123 我们刚才所设置的)。

然后他会返回一系列的数据,这里我就粘贴一部分啦:


CONNECTED(00000003)
depth=1 /C=US/O=Entrust, Inc./OU=www.entrust.NET/rpa is incorporated by reference/OU=(c) 2009 Entrust, Inc./CN=Entrust Certification Authority - L1C
verify error:num=20:unable to get local issuer certificate
verify return:0
---
Certificate chain
。。。。。(省略)
。。。。。(省略)
。。。。。(省略)
  Start Time: 1416389389
  Timeout  : 300 (sec)
  Verify return code: 0 (ok)
---

测试就到这里啦。。。

第七部分

1.建立推送项目


// 
// AppDelegate.m 
// TestPushNotifiy 
// 
// Created by silicon on 14-10-30. 
// Copyright (c) 2014年 silicon. All rights reserved. 
// 
 
#import "AppDelegate.h" 
 
@implementation AppDelegate 
@synthesize mainView = _mainView; 
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
  if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) 
  { 
    //IOS8 
    //创建UIUserNotificationSettings,并设置消息的显示类类型 
    UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:nil]; 
     
    [application registerUserNotificationSettings:notiSettings]; 
     
  } else{ // ios7 
    [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge                    |UIRemoteNotificationTypeSound                   |UIRemoteNotificationTypeAlert)]; 
  } 
   
  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
  // Override point for customization after application launch. 
  self.window.backgroundColor = [UIColor whiteColor]; 
  [self.window makeKeyAndVisible]; 
   
  self.mainView = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil]; 
  self.window.rootViewController = self.mainView; 
  return YES; 
} 
 
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken{ 
  NSLog(@"---Token--%@", pToken); 
} 
 
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ 
   
  NSLog(@"userInfo == %@",userInfo); 
  NSString *message = [[userInfo objectForKey:@"aps"]objectForKey:@"alert"]; 
   
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil nil]; 
   
  [alert show]; 
} 
 
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{ 
 
  NSLog(@"Regist fail%@",error); 
} 
 
- (void)applicationWillResignActive:(UIApplication *)application 
{ 
  // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
  // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
} 
 
- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
  // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.  
  // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
} 
 
- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
  // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 
} 
 
- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
  // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
} 
 
- (void)applicationWillTerminate:(UIApplication *)application 
{ 
  // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
} 
 
@end