Retrieving token from APNS-php error
I am trying to implement APNS-PHP, and discovered that in my test environment I have a few invalid tokens (as the test devices moved to production).
I need to get the token ID from a serialized object in an array, as I want to catch this scenario and remove the invalid tokens form the DB. I use the following code, but that doesn't work:
$aErrorQueue = $push->getErrors();
if (!empty($aErrorQueue)) {
foreach($aErrorQueue as $error){
foreach($error['ERRORS'] as $err){
$message .= $err['statusMessage'] . " ";
if($err['statusCode'] == 8){
$phones = Phone::getPhonesWithToken($error['MESSAGE']['_aDeviceTokens:protected'][0]);
Phone::setToken($phones[0]['id'], "");
}
}
}
}
The problem is that the APNS_Message is the serialized object in $error['MESSAGE'], and I cannot remember how to access the token in that object...
Var dump:
["MESSAGE"]=> object(ApnsPHP_Message)#9 (8) { ["_bAutoAdjustLongPayload:protected"]=> bool(true) ["_aDeviceTokens:protected"]=> array(1) { [0]=> string(64) "018E开发者_StackOverflow社区4B9CB8CF73341CE4EBE7138E25E605CD80FB74B3A9701CE5CCA6D9363F3A" } ["_sText:protected"]=> NULL ["_nBadge:protected"]=> int(256) ["_sSound:protected"]=> NULL ["_aCustomProperties:protected"]=> NULL ["_nExpiryValue:protected"]=> int(604800) ["_mCustomIdentifier:protected"]=> string(17) "Message-Badge-004" }
_aDeviceTokens
is a protected property, you will find that accessing this property directly will throw an exception.
You should instead use the getRecipients()
or getRecipient($recipientNumber = 0)
method on the Message
object to retrieve the device token(s).
For example:
$token = $error['MESSAGE']->getRecipient();
$error['MESSAGE']->_aDeviceTokens[0]
精彩评论