推送

2016-10-22  本文已影响10人  学而不思则罔思而不学则殆

APNS (Apple Push Notification Service) 推送机制

苹果官方图

APNS推送通知的详细工作流程

APNS推送通知的详细工作流程

准备工作

使用 Push

// 在AppDelegate里didFinishLaunchingWithOptions函数里写
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //推送的形式:标记,声音,提示
   [[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
   return YES;
}

- (void)application:(UIApplication *)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken {
   NSLog(@"regisger success:%@",pToken);
   //注册成功,将deviceToken保存到应用服务器[数据库](http://lib.csdn.net/base/14)中
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    // 处理推送消息
    NSLog(@"userinfo:%@",userInfo);

    NSLog(@"收到推送消息:%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
}
- (void)application:(UIApplication *)applicationdidFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
   NSLog(@"Registfail%@",error); 
}
上一篇 下一篇

猜你喜欢

热点阅读