iOS通知用法

转http://m.oschina.net/blog/166973

要测试通知需要两个条件:真机和开发者帐号,不然没法测试。

对照如上图我们介绍文章后面的一些概念

1、Provider: 就是对应后面介绍的  PushMeBaby工程(用来向APNS发送消息)

2、APNS: Apple_Push_Notification_Service 苹果消息推送服务器

3、iphone:用来接受APNS下发下来的消息推送

4、Client App: 对应后面提到的 PushClient 工程,安装在iphone上面的程序 (用来接收iphone传递APNS下发的消息到制定的一个客户端 app)

-------------------------------------------------------------------------------------------------------------------------------------------------------

介绍完以后几个概念以后,现在先申明下在以下的工程配置中要用到 苹果开发者帐号 去下载 aps_developer_identity.cer 然后重命名apns.cer放在 pushMeBaby工程中才可以顺利的配置好推送的一些功能。

----------------------------------------------------------------------------------------------------------------------------------------------------

现在进入正题

1、获取deviceToken

在 PushClient工程的的main.m文件中的

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

方法中获取到 deviceToken,用来填充在 PushMeBaby中向服务器发送信息
2、获取在服务端所要的许可证 (要用到开发者帐号中的相关配置)
  1、    首先登录https://developer.apple.com/devcenter/ios/index.action  登录帐号
         

    2、 

         

   3、 

    4、下载 push ssl certificate 把原文件名称aps_developer_identity.cer -------->重命名为apns.cer

  5、下载下来的testpush.mobileprovision配置到工程中

前期的工程配置和一些证书获取完毕以后我们要下载工程文件PushMeBaby 和 PushClient

下载地址:

http://download.csdn.net/source/3182374

下载完成以后我们要做两个事情

1、把刚才获取的apns.cer文件拷贝到pushMeBaby工程文件同级目录下

2、把 pushClient安装到真机上运行后获取 deviceToken

(其实就是 push client中

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

{

}

中的deviceToken

)

记住然后在工程 pushMeBaby中用刚才获取的40为字符替换掉在

ApplicationDelegate.m

-(id)init

{

...

self.deviceToken = "刚才获取的deviceToken";

}

运行工程后如图

其中如图上面的Device Token就是要push的机子

然后运行pushMeBaby运行正常的话就可以看到如下的效果,APNS

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    // 启动时接收挂起时候收到的通知
    NSDictionary *remoteDic = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 30)];
    lable.tag = 101;
    lable.backgroundColor = [UIColor redColor];
    lable.textColor = [UIColor whiteColor];
    if (remoteDic) {
        lable.backgroundColor = [UIColor purpleColor];
        lable.text = [[remoteDic objectForKey:@"aps"] objectForKey:@"alert"];
    }
    [self.window addSubview:lable];
    
    // 注册通知
    [application registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert];

    return YES;
}

// 接收到token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0) {
    

    NSLog(@"deviceToken: %@", deviceToken);
    
}
// 发生错误
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error NS_AVAILABLE_IOS(3_0) {
    NSLog(@"%@", error);
}

// 接收到远程通知,程序运行中接收
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo NS_AVAILABLE_IOS(3_0) {
    NSLog(@"%@", @"didReceiveRemoteNotification");

    UILabel *lable = (UILabel *)[self.window viewWithTag:101];
    lable.backgroundColor = [UIColor blackColor];
    lable.text = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
    NSLog(@"%@", userInfo);
}
// 接收到本地通知
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0){
    NSLog(@"%@", @"didReceiveLocalNotification");
    NSLog(@"%@", notification);

}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // 活动的时候取消ICO图标数字
    [application setApplicationIconBadgeNumber:0];
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值