手把手教你如何實現蘋果推送通知

本文參考自
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.點擊這裏下載PushMeBabywritten 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. 終於看到最後的成果了




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章