- (BOOL)application:(UIApplication
*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
- // Override point for customization after application launch.
- self.viewController = [[[ViewController alloc] init] autorelease];
- self.window.rootViewController = self.viewController;
- [self.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
- [self.window makeKeyAndVisible];
- /** 注册推送通知功能, */
- [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
- //判断程序是不是由推送服务完成的
- if (launchOptions) {
- NSDictionary* pushNotificationKey = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
- if (pushNotificationKey) {
- UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"推送通知"
- message:@"这是通过推送窗口启动的程序,你可以在这里处理推送内容"
- delegate:nil
- cancelButtonTitle:@"知道了"
- otherButtonTitles:nil, nil];
- [alert show];
- [alert release];
- }
- }
- return YES;
- }
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
- NSString* token = [NSString stringWithFormat:@"%@",deviceToken];
- NSLog(@"apns -> 生成的devToken:%@", token);
- //把deviceToken发送到我们的推送服务器
- DeviceSender* sender = [[[DeviceSender alloc]initWithDelegate:self ]autorelease];
- [sender sendDeviceToPushServer:token ];
- }
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
- NSLog(@"apns -> 注册推送功能时发生错误, 错误信息:\n %@", err);
- }
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
- {
- NSLog(@"\napns -> didReceiveRemoteNotification,Receive Data:\n%@", userInfo);
- //把icon上的标记数字设置为0,
- application.applicationIconBadgeNumber = 0;
- if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]!=NULL) {
- UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"**推送消息**"
- message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]
- delegate:self
- cancelButtonTitle:@"关闭"
- otherButtonTitles:@"处理推送内容",nil];
- alert.tag = alert_tag_push;
- [alert show];
- }