gpt4 book ai didi

iphone - ios上的实时推送通知教程

转载 作者:行者123 更新时间:2023-11-28 20:31:44 27 4
gpt4 key购买 nike

我已经尝试了很多教程来为我的应用程序发送推送通知服务。当我在设备上测试它时,它可以正常工作。但是如果我在 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);
}

有没有教程可以教我把它放在live上。我把我的php文件的环境从sandbox改成了live。请指导。

最佳答案

我自己解决了这个问题。

不在 Live 上运行的原因是因为

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

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

- (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");

}

关于iphone - ios上的实时推送通知教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11931700/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com