4

For at least the past couple of days (maybe longer), nothing seems to happen when I send an Empty Push Notification to at least 1 test device. I'm pretty much using the same SendEmptyPushNotification code as the article posted below.

https://medium.com/@yangzhoupostbox/part-2-push-notification-for-updating-apple-pass-in-asp-net-6020768d112

My test device is running iOS 14.4.1. I can manually update the Pass, so the WebServiceURL is working.

My cert expires later this year. I've rebooted my device. This all used to work and I haven't changed anything.

NOTE: It looks like the AppleDeviceLibraryIdentifier & ApplePushToken are changing over time. Should these change or remain the same over the life of the Apple device the passes are installed on? I'm wondering if the new PushToken could be the issue. This seems to be the expected behavior. Does the Device Library Identifier change with the Push Token?

Does anyone know what might be wrong? Is anyone else having an issue?

Dumber_Texan2
  • 840
  • 2
  • 12
  • 34

2 Answers2

4

Me too man. Let's figure this out together like Apex Legends.

I have submitted a TSI to Apple. They sent out an email on feb 10 saying:

On March 29, 2021, token and certificate-based HTTP/2 connections to the Apple Push Notification service must incorporate the new root certificate (AAACertificateServices 5/12/2020) which replaces the old GeoTrust Global CA root certificate. To ensure a seamless transition and to avoid push notification delivery failures, verify that both the old and new root certificates for the HTTP/2 interface are included in the Trust Store of each of your notification servers before March 29.

Note that Apple Push Notification service SSL provider certificates issued to you by Apple do not need be to updated at this time.

Learn more about connecting to APNs.

If you have any questions, contact us.

Best regards, Apple Developer Relations

Update - Mon May 3, after submitting a TSI to Apple Dev

  1. Push notifications stopped working for developers on March 31, 2021 after Apple migrated to a new APNS provider API (http/2 protocol).

  2. To continue using push, see this page: https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns/

What I learned?

Revoke all developer account certificates related to APNS Make new certs and this time don't make any PEM files when installing them to your providing server. Also, make sure to stop using port 2195 when making a connection to APNS and use 443 or 2197.

The great news? The new APNS provider API is still compatible with Objective C!

<?php
// Put your device token here (without spaces):
$deviceToken = '09a0c8ff92b3621c5f5032c1fa031f2851d01dd99beaf1446c281c5458fe2ffd';
// Put your private key's passphrase here:
$passphrase = 'pushchat';
// Put your alert message here:
$message = 'Hello!';

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', 
strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);

Update: I was wrong about the PEM aspect. Creating a PEM file is still necessary for APNs connections using the new APNs Provider API.

Pang
  • 9,564
  • 146
  • 81
  • 122
programcode
  • 104
  • 1
  • 14
  • 1
    Glad I'm not the only one having the issue. Here is a link that might be useful. https://developer.apple.com/support/expiration/ If you click on renewed certificate under Taking Action, it downloads a new Apple Worldwide Developer Relations Certification Intermediate Certificate. I am currently using the one that expires in 2023. I assume this is the problem, but we don't use the Intermediate Certificate when sending an Empty Push Notification. Hmmm??? I added a new Private Key in November of 2020, so I don't know if this change really impacts me. So confusing! – Dumber_Texan2 Apr 29 '21 at 17:07
  • 1
    I guess we will have wait on a response from Apple to your TSI. Here's more info. "Apple Push Notification service and Pass Type ID certificates Apple Push Notification service certificates including the Pass Type ID certificate will be updated in fall 2021 and will be associated to a new intermediate certificate focused on Push Notification services. At this time, we will be reducing the number of certificates required to interact with APNs. Until this update, continue to use the existing intermediate certificate, which expires in February 2023." – Dumber_Texan2 Apr 29 '21 at 17:22
  • 1
    Heyo. Hope you had a good weekend. I updated my answer. – programcode May 04 '21 at 02:05
  • 1
    @programcode do you have any code samples for triggering the push notification from the server? I was simply referencing the PEM file in my stream like in this example: https://gist.github.com/samvermette/759564/898811d7509b12021ad248cc7956896a92c5424f ....but it appears that won't work anymore. And you mentioned no longer using PEM files, so I'm trying to figure out the correct stream code AND the correct certificate/PEM setup. Any help is appreciated! – jberg7777 May 05 '21 at 17:25
  • Yes I do. I use a similar version. Update in progress. – programcode May 05 '21 at 21:27
  • @jberg7777 will be home in a few hours so I can share the file from where it is saved. It no longer triggers notifications from the server but this was to be expected as the certificate process is slightly different now. In the mean time Apple shared this link for sending push notifications via command line tools. https://developer.apple.com/documentation/usernotifications/sending_push_notifications_using_command-line_tools – programcode May 05 '21 at 22:22
  • Late night last night. Just posted the file I use to use to trigger remote notifications from my server. – programcode May 06 '21 at 21:20
  • 1
    @programcode are you updating Apple Passes or sending push notifications to apps running on user devices? My new code is working, but the Pass is not updating. This article doesn't even mention passes. https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns/ – Dumber_Texan2 May 11 '21 at 21:26
  • 1
    @programcode I'm trying to connect via the certificate route. I have it working on my server via command line using the curl examples provided by apple (so I know HTTP/2 is working), but looks like PHP is behaving differently but something is failing silently. According to apple, they require the path to include "/3/device/"...and they also require "apns-push-type" and "apns-topic" headers (which I included in my working curl example). However, I don't see any references to these in your code. Are you passing them in somewhere else? – jberg7777 May 12 '21 at 00:10
  • 1
    I finally got it all working for Pass updates using Asp.Net 7.2. I will post my answer tomorrow. For anyone updating Passes, read this. https://stackoverflow.com/questions/67494713/how-to-get-apple-pass-updates-to-work-using-the-new-apns-http2-process/67495800#67495800 – Dumber_Texan2 May 12 '21 at 00:19
  • 1
    @Dumber_Texan2 can you share your php code with how you are successfully triggering the notification? I have it working in curl on my server, but not sure why it wouldn't work using the php stream mentioned here...and apparently there's no easy way to access the responses from Apple within the stream via php – jberg7777 May 12 '21 at 00:27
2

To get this to work in a non .Net Core Solution, I had to upgrade from Asp.net 4.6.1 to 4.7.2. I then created a folder in my Project called AppleAPNS. I added all the files found here.

NOTE: I had to upgrade to 4.7.2 in order to get the Project to recognize CreateFromValue found in the AppleCryptoHelper file.

https://github.com/andrei-m-code/net-core-push-notifications/tree/master/CorePush/Apple

I then added the files found here to the AppleAPNS folder.

https://github.com/andrei-m-code/net-core-push-notifications/tree/master/CorePush/Utils

I then added the WinHttpHandler Project along with this helper to my AppleAPNS folder. This will ensure that you are calling APNS with an HTTP client capable of 2.0.

public class Http2CustomHandler : WinHttpHandler
    {
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
        {
            request.Version = new Version("2.0");

            return base.SendAsync(request, cancellationToken);
        }
    }

Next, add the AppleNotification class found here and use the calling method in the program.cs file.

https://github.com/andrei-m-code/net-core-push-notifications/tree/master/CorePush.Tester

To use the Http2CustomHandler, make these changes.

ApnSender.cs

//private readonly HttpClient http;
        private readonly HttpClient http = new HttpClient(new Http2CustomHandler());

In your calling method make this change.

//private static readonly HttpClient http = new HttpClient();
        private static readonly HttpClient http = new HttpClient(new Http2CustomHandler());

Finally, make this change to send an empty push notification to update passes.

ApnSender.cs

//var json = JsonHelper.Serialize(notification);
            var json = "{\"aps\":\"\"}";

NOTE: Copy the contents of the p8 file as string into the apnP8PrivateKey in your calling method. Do not open/read the file.

Hope this helps! I know this is not a detailed explanation, but hopefully it will help somebody.

Dumber_Texan2
  • 840
  • 2
  • 12
  • 34