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

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

问题描述

是否有示例项目展示了如何在 IPhone 上集成 APNS,以及如何获取 deviceToken?

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. 在您的应用程序委托的 didFinishLaunchingWithOptions 中,您应该注册远程通知.请注意,Apple 的文档建议在每次应用程序运行时进行注册,因为令牌可能会不时更改.您可以通过调用:

  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];

  • 注册远程通知后,将调用已传递令牌的应用程序委托中的方法,您需要在应用程序委托中实现此方法并将令牌发送到您的服务器(这将向您发送通知).该方法将如下所示:

  • 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];
    }
    

  • 你也应该实现这个:

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

    1. 您需要在收到通知后进行处理.处理收到的通知的场景很少(应用程序在后台或前台等),如果应用程序在前台接收通知时处理通知的方法应该在应用程序委托中实现.就是这样:

    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
    }
    

    要了解有关 userInfo 结构的更多信息并涵盖所有不同的场景,请仔细阅读 http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html.这只是事情的要点:)

    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 应用程序上实现 Apple 推送通知服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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