如何获取设备令牌 [英] How to get device tokens
问题描述
我有一个适用于 iOS 的应用程序,我想在那里集成推送通知.我看过 youtube 上的教程,一切正常,但最近我正在使用开发证书(用于测试 - 不适用于 AppStore),我的服务器上有 PHP 脚本.在这个文件中存储了我的 iPhone 的 deviceToken,它写在 php 变量 $deviceToken 中.但是现在,当我想在 AppStore 中使用它时,如何从下载我的应用程序的每个人那里获取设备令牌并将其放入 PHP 脚本中?
I have an app for iOS and I want to integrate there push notifications. I have seen a tutorial on youtube and everything is OK, but recently I'm using a development certificate (for testing - not for AppStore use) and I have PHP script on my server. In this file is stored the deviceToken which have my iPhone and it is written in php variable $deviceToken. But now, when I want to use this in AppStore, how can I get the device tokens from everybody who had downloaded my app and get it into the PHP script?
这是我的 PHP 文件:
This is my PHP file:
if($_POST['message']){
$deviceToken = '(my device token)';
$message = stripslashes($_POST['message']);
$payload = '{
"aps" :
{ "alert" : "'.$message.'",
"badge" : 1,
"sound" : "bingbong.aiff"
}
}';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'cert.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', 'password');
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if(!$fp){
print "Failed to connect $err $errstrn";
return;
} else {
print "DONE!";
}
$devArray = array();
$devArray[] = $deviceToken;
foreach($devArray as $deviceToken){
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack ("n",strlen($payload)) . $payload;
fwrite($fp, $msg);
}
fclose($fp);
}
<form action="send-notification.php" method="post">
<input type="text" name="message" maxlength="100">
<input type="submit" value="SEND">
</form>
</body>
这就是我在 xCode (AppDelegate.m) 中所拥有的
and this is what I have in xCode (AppDelegate.m)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *deviceTokenString = [NSString stringWithFormat:@"%@", deviceToken];
NSLog(deviceTokenString);
}
推荐答案
嗯,我不懂PHP,所以不能给你具体的代码,但我可以解释一下大体原理.
Well, I don't know PHP, so I can't give you specific code, but I can explain the general principle.
当您的应用程序启动时,您应该通过调用 registerForRemoteNotificationTypes:
方法注册到 Apple 推送通知.
When your application starts you should register to Apple Push Notifications by calling the registerForRemoteNotificationTypes:
method.
当注册成功并获得设备令牌时,您应该在application:didRegisterForRemoteNotificationsWithDeviceToken:
的实现中将其发送到您的服务器.
When the registration succeeds and you get the device token, you should send it to your server in the implementation of application:didRegisterForRemoteNotificationsWithDeviceToken:
.
您的服务器应该将其存储在某个数据库中.
Your server should store it in some data base.
发送通知的 PHP 脚本应该从该数据库中获取设备令牌.
Your PHP script that sends the notifications should get the device tokens from that data base.
这篇关于如何获取设备令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!