APNS推送通知后台不再起作用

时间:2020-03-09 09:37:02

标签: ios push-notification apple-push-notifications ios13 silentpush

很抱歉,由于多次询问,该问题已被重新发布,但对我来说没有答案。 我的应用程序不再在后台通知中唤醒。 因此,我更新了我的PHP脚本并添加了herehere所述的HEADERs字段 但是没有运气。

这是我的示例代码:

<?php

$device_token = 'd98e1d488acac56305a9f83b...b616bc5b5b3bf238dc'; 
//$device_token = '27b296397f81f48590a....fe2742e0b2a051ae2cefffded85d7';

// Put your private key's passphrase here:
$pem_secret = '';
$pem_file = 'pushcert.pem';
$url = "https://api.development.push.apple.com/3/device/$device_token";


//$body = '{"aps":{"alert":"balbla","badge":0,"sound":"default"}}';
$body = '{"aps":{"content-available":1},"acme1":"bar"}';
$header = ['apns-topic'=>'com.blabla.bla',
    'apns-push-type'=>'background',
    'apns-priority'=>'5'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_SSLCERT, $pem_file);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $pem_secret);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


$information = curl_getinfo($ch);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

//On successful response you should get true in the response and a status code of 200
//A list of responses and status codes is available at 
//https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1
//var_dump($header);
//var_dump($information);
var_dump($response);
var_dump($httpcode);

?>

我从不进入iOS方法来捕获此通知:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[[NSUserDefaults standardUserDefaults] setValue:userInfo forKey:@"testtesttest"];
// Method called when user click on notification and application is in background
application.applicationIconBadgeNumber = 0;
NSLog(@"did received remote notification");  }

也在我的设备控制台中,该设备似乎正在取消我的通知或未收到的通知。 enter image description here

标准通知(不是在后台)也不能正常工作。 测试iOS 13.3.1

非常感谢您能为我提供的帮助。

启用背景模式:

<key>UIBackgroundModes</key>
<array>
    <string>fetch</string>
    <string>location</string>
    <string>processing</string>
    <string>remote-notification</string>
</array>

enter image description here

“应用程序设置”也已配置: enter image description here

1 个答案:

答案 0 :(得分:0)

我设法使其与该示例代码一起使用:

<?php

// open connection
if (!defined('CURL_HTTP_VERSION_2_0')) {
    define('CURL_HTTP_VERSION_2_0', 3);
}
$device_token = 'd56e9ef8b5ea558f07584cf8...498824ece60a36229446a2003116'; //David

// Put your private key's passphrase here:
$pem_secret = '';
$pem_file = 'pushcert.pem';
$url = "https://api.development.push.apple.com/3/device/$device_token";
$app_bundle_id = "com.blabla.blabla";

//$body = '{"aps":{"alert":"balbla","badge":0,"sound":"default"}}';
$body = '{  
            "aps":{"content-available":1},
            "type":"LOCATION",
            "idRequest":"538EAC3E765A4BDC89B554C40F5B14EF"
        }';

$headers = array(
    "apns-topic: {$app_bundle_id}",
    "User-Agent: My Sender",
    "apns-push-type:background",
    "apns-priority:5"
);

 var_dump($body);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PORT,443);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST,TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSLCERT, $pem_file);
curl_setopt($ch, CURLOPT_HEADER, TRUE);

$response = curl_exec($ch);
$information = curl_getinfo($ch,CURLOPT_HTTPHEADER);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

//On successful response you should get true in the response and a status code of 200
//A list of responses and status codes is available at 
//https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1
//var_dump($header);
var_dump($information);
var_dump($response);
var_dump($httpcode); 

?>

至少看起来应用程序正在唤醒(在设备日志中可见)

  • 所以今天早上,我从昨天开始运行代码(完全没有修改),并且运行了几次,例如4到5次。
  • 现在它不再起作用了。与昨天相同(iOS系统似乎捕获了有效负载,但应用程序未唤醒)

->结论,这是系统限制,当您需要执行测试时,这是如此令人困惑。我要把它投入生产,不能保证它能正常工作...