2013年4月8日 星期一

遠端推播

http://www.easyapns.com/
 //官方文件的中文翻譯版
http://blog.toright.com/archives/2806

//接收端


//在程式開始時註冊
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
     //Registering for push notifications...
    [[UIApplication sharedApplication]
     registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeAlert |
      UIRemoteNotificationTypeBadge |
      UIRemoteNotificationTypeSound)];
    return YES;
}


//要在AppDelegate中實作下面三個delegate
#pragma mark ﹣ Push Notification Delegate

//完成註冊後,必須傳送device token等的相關資訊給你的provider(server)。token是必要的,因為APNs會利用token跟device做溝通,當然,你也可以傳送些額外的資訊。
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString *str = [NSString stringWithFormat:@"Device Token=%@",deviceToken];      
    NSLog(@"%@",str);
}

//傳送過程中若有發生錯誤,會反應在底下這個method。
 - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
     NSString *str = [NSString stringWithFormat: @"Error: %@", err]; NSLog(@"%@",str);
}

//APP正常運行或背景執行時:
//主要都是利用這個method做處理,只是需要經過ㄧ些判斷。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    for (id key in userInfo) {
       NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
    }
}

沒有留言:

張貼留言