ios上的实时推送通知教程

Posted

技术标签:

【中文标题】ios上的实时推送通知教程【英文标题】:Live push notification tutorial on ios 【发布时间】:2012-08-13 09:42:33 【问题描述】:

我已经尝试了很多教程来为我的应用程序发送推送通知服务。当我在设备上测试它时它可以工作。但是如果我在我的应用程序在 App Store 上启动后对其进行实时测试,并且我通过从商店下载将它安装在我的设备上,我运行 php 文件以发送推送通知,我没有收到它。这是代码我用于推送通知和我从中学到的tutorial。

- (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] initWithNibName:@"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

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




return YES;



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

NSLog(@"My token is: %@", deviceToken);



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

NSLog(@"Failed to get token, error: %@", error);

有没有什么教程可以教我实时使用它。我将我的 php 文件的环境从沙箱更改为实时。请指导。

【问题讨论】:

您是否为生产服务器使用了正确的 ssl 证书?您不能使用沙盒证书连接到生产服务器。 是的,我为生产服务器使用了正确的 ssl 证书.. @Sascha:我的编码正确吗? 您是否在应用程序中将 deviceToken 发送到您的服务器:application didRegisterForRemoteNotificationsWithDeviceToken: 【参考方案1】:

我自己解决了这个问题。

没有在 Live 上工作的原因是因为

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

在这种方法中,设备令牌未正确注册到我的 php 服务器数据库。以下代码将有助于在 php 服务器中注册设备令牌

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

NSLog(@"My token is: %@", deviceToken);

NSString* newToken = [deviceToken description];

newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];

NSLog(@"%@",newToken);
NSString *urlString = [NSString stringWithFormat:@"http://sample.com/registerDevice.php?appId=xxx&deviceToken=%@",newToken];

NSURL *url = [[NSURL alloc] initWithString:urlString];

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

NSData *urlData;
NSURLResponse *response;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
NSLog(@"data send");


【讨论】:

【参考方案2】:

首先,我认为使用开发推送通知证书(或临时证书)安装应用程序、获取 deviceToken、安装应用商店应用程序并使用以前的设备令牌发送推送通知都行不通。

此链接确认:iPhone APNS Device Tokens in sandbox vs. production

这可能解释了为什么您无法向您的应用程序发送推送通知。

另外,您需要将设备令牌发送到您的服务器,因为这是您的服务器知道所有设备令牌的唯一方法。

我建议您查看apple documentation。

【讨论】:

以上是关于ios上的实时推送通知教程的主要内容,如果未能解决你的问题,请参考以下文章

推送通知上的三个按钮

免费苹果帐户上的 ios 推送通知开发

iOS 上的重复推送通知

处理锁屏ios上的推送通知

Linphone如何支持Android / iOS上的推送通知

iP6s 上的 iOS 推送通知