手把手教你如何实现苹果推送通知

来源:互联网 发布:红后 知乎 编辑:程序博客网 时间:2024/05/18 18:04

本文参考自
http://mobiforge.com/developing/story/programming-apple-push-notification-services
感谢WEIMENGLEE 简单又易懂的教程



废话不说,直接进入主题


生成请求证书

1. 首先打开 Keychain Access,进入后选择“'Certificate Assistant”——>"Request a Certificate From a Certificate Authority"


2.填写好信息后按下一步


3.直接使用默认名并把证书保存到桌面




创建APP ID


1.登陆http://developer.apple.com/iphone/,在右侧选择" iPhone Developer Program Portal"



2.点击左侧的APP IDS选项,选择“NEW APP ID”创建新的的ID


3.在Description 里填上你要的名字,这里我们用"PushAppID",在Bundle Seed ID选择“ Generate New”,Bundle Identifier,我们使用net.learn2develop.MyPushApp.然后点击提交



4.提交后,我们可以看到新生成的APP ID



配置推送通知


1.在刚生成的APP ID右侧点击“Configure "按钮,勾选"Enable for Apple Push Notification service",点击”Development Push SSL Certificate“的“Configure "按钮


2.点击后进入这个页面,点击继续


3.上传刚刚保存在桌面的证书,点击生成


4.成功后按继续,这时我们就可以看到新生成的SSL证书了,点击下载并命名aps.developer.identity.cer


5.下载完双击它


6. 回到” iPhone Development Program Portal“页面,在左侧点击Provisioning,点击New Profile,命名为MyDevicesProfile ,在App ID里选择刚刚创建的PushAppID,点击提交,生成完后点击下载并命名为MyDevicesProfile



好了,前面的准备工作已经OK,下面进入编程环节


配置设备

1.把你的Iphone 或iPod Touch连接到电脑
2.把MyDevicesProfile.mobileprovision双击打开或拖入XCode,它会自动加载到Organizer 里


创建新项目

1.创建一个View-Based项目,我们命名为ApplePushNotification

2.拖入一个 WAV类型的声音文件,这里我们使用beep.wav(没有的自己找个)


3.在Target选择ApplePushNotification 项,Command-I选择Properties,在Identifier 里填入net.learn2develop.MyPushApp

4.选择Build,在 "Code Signing"里选择你连接的设备



5.在ApplePushNotificationAppDelegate.m里,填入一下代码

#import "ApplePushNotificationAppDelegate.h"#import "ApplePushNotificationViewController.h" @implementation ApplePushNotificationAppDelegate @synthesize window;@synthesize viewController; - (void)applicationDidFinishLaunching:(UIApplication *)application {        [window addSubview:viewController.view];    [window makeKeyAndVisible];     NSLog(@"Registering for push notifications...");        [[UIApplication sharedApplication]         registerForRemoteNotificationTypes:        (UIRemoteNotificationTypeAlert |          UIRemoteNotificationTypeBadge |          UIRemoteNotificationTypeSound)]; } - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {      NSString *str = [NSString         stringWithFormat:@"Device Token=%@",deviceToken];    NSLog(str); } - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {      NSString *str = [NSString stringWithFormat: @"Error: %@", err];    NSLog(str);     } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {     for (id key in userInfo) {        NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);    }     } - (void)dealloc {    [viewController release];    [window release];    [super dealloc];} @end


6. Command-R运行程序,复制后台生成的 device token 


7.确认你的Iphone打开了通知服务,运行程序后你可以看到增加了一个新的服务


8.点击这里下载PushMeBaby(written by Stefan Hafeneger)用于测试

9.打开PushMeBaby,把刚刚生成的SSL证书拖入项目


10.打开ApplicationDelegate.m,键入以下代码
- (id)init {    self = [super init];    if(self != nil) {        self.deviceToken = @"38c866dd bb323b39 ffa73487 5e157ee5 a85e0b7c e90d56e9 fe145bcc 6c2c594b";         self.payload = @"{\"aps\":{\"alert\":\"You got a new message!\",\"badge\":5,\"sound\":\"beep.wav\"},\"acme1\":\"bar\",\"acme2\":42}";         self.certificate = [[NSBundle mainBundle]             pathForResource:@"aps_developer_identity" ofType:@"cer"];    }    return self;}


11. Command-R运行项目,选择 Always Allow 然后点击Push按钮



12. 终于看到最后的成果了