我如何能实现在iOS应用苹果推送通知服务? [英] How can I implement Apple Push Notification Service on iOS Application?

查看:139
本文介绍了我如何能实现在iOS应用苹果推送通知服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有说明如何 APNS 集成了 IPhone任何示例项目,以及如何获得deviceToken?


解决方案

有你需要遵循几个简单的步骤:


  1. 在您的应用程序委托的didFinishLaunchingWithOptions您应该为远程通知登记。注意到,苹果的单证表明每个应用程序运行,因为令牌可能会随时改变时间注册。通过调用做到这一点:

      [UIApplication的sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];


  2. 在您的应用程序委托将被调用它已经通过了标记注册远程通知的方法后,你需要实现应用程序委托此方法并发送令牌到你的服务器(即会向你发送通知)。该方法将是这样的:

       - (无效)应用:(*的UIApplication)的应用didRegisterForRemoteNotificationsWithDeviceToken:(NSData的*)deviceToken {    的NSLog(@设备令牌:%@,deviceToken);
        [服务器sendToken:deviceToken];
    }


您也应该实现这个还有:

   - (无效)应用:(*的UIApplication)的应用didFailToRegisterForRemoteNotificationsWithError:(NSError *)错误{}


  1. 您需要,一旦你让他们来处理通知。有在你处理收到的通知(应用程序在后台或前台等),处理通知,如果应用程序是在前台当您收到它应该在应用程序的委托实施该方法几个不同的方案。这是它:

       - (无效)应用:(*的UIApplication)的应用didReceiveRemoteNotification:(NSDictionary的*){用户信息
        的NSLog(@接到通知);
        //这里办理通知
    }


要了解更多有关用户信息结构和覆盖所有仔细阅读<一个不同的场景href=\"http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html\">http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html.这只是事情的要点:)

Is there any sample project showing how to integrate APNS on IPhone, and how to get deviceToken?

解决方案

there are a few simple steps you need to follow:

  1. in your app delegate's didFinishLaunchingWithOptions you should register for remote notifications. notice that apple's documentations suggests to register each time the app runs since the token may change from time to time. you do this by calling:

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
    

  2. after registering for remote notifications a method in your app delegate will be called which had been passed the token, you need to implement this method in your app delegate and send the token to your server (that will send you notifications). the method will look like this:

    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    
        NSLog(@"device token is: %@",deviceToken);
        [server sendToken:deviceToken];
    }
    

you should also implement this as well:

    -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{}

  1. you need to handle notifications once you get them. there are few different scenarios in which you handle received notifications (the app is in the background or foreground etc.), the method that handles a notification if the app is in the foreground when you receive it should be implemented in the app delegate. this is it:

    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
        NSLog(@"received notification");
        //handle the notification here
    }
    

to learn more about the userInfo structure and cover all the different scenarios read carefully http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html. this was just the gist of things :)

这篇关于我如何能实现在iOS应用苹果推送通知服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆