1

很抱歉重新发布这个问题,因为它被问了几次,但对我来说没有答案是可以的。我的应用不再在后台通知中唤醒。所以我更新了我的 php 脚本并添加了 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");  }

同样在我的设备控制台中,设备似乎正在取消我的通知或没有收到的通知。 在此处输入图像描述

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

非常感谢你能给我的任何帮助。

启用后台模式:

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

在此处输入图像描述

应用程序设置还配置: 在此处输入图像描述

4

1 回答 1

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系统似乎捕捉到了payload,但应用程序没有被唤醒)

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

于 2020-03-09T17:54:12.407 回答