我有一個應用程序iOS我想集成推送通知。我見過tutorial on youtube,一切正常,但最近我正在使用開發證書(用於測試 - 不適用於AppStore的使用),並且我的服務器上有PHP腳本。在這個文件中存儲了具有我的iPhone的deviceToken,它是用php變量$ deviceToken寫的。但是現在,當我想在AppStore中使用它時,如何從每個下載了我的應用程序並將其放入PHP腳本的人獲取設備令牌?如何獲得設備令牌
這是我的PHP文件:
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)
- (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);
}
哦,所以我需要將每個設備標記存儲在數據庫中,而不是我必須對它們進行排列併發送它。你能解釋一下,我怎麼能把devicetoken從xcode發送到我的服務器? Thx非常多, Steve – stepik21
@ stepik21從xcode發送設備令牌意味着什麼?您必須在聯繫您的服務器併發送設備令牌的應用程序中編寫代碼。例如,您可以將HTTP POST或HTTP GET請求發送到服務器,並將設備令牌作爲參數。 – Eran
哦,是的,我從應用程序meen ...好吧,我會嘗試它:-) – stepik21