Request APNs VoIP notification (Flutter iOS)

2,227

Solution 1

The problem is that you are not using the voip certificate to authenticate your HTTP request. See that you are using that on your curl request with the --cert command. I don't know how to do that with http.post, but you need the private key of the certificate to use it to sign your request. https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/establishing_a_certificate-based_connection_to_apns

However, you should not be sending pushes through your app, because it means you will need to pack your voip certificate private key with your application. With some sort of reverse engineering anyone can get your private key, and send pushes to your apps. The private key should be kept safe inside your server, and never be shared as your are planning to do.

Solution 2

Solved.

Below a code example of VoIP Push using node.js and "apn" module.

var apn = require("apn");
var deviceToken = "*yourDeviceToken*";
let provider = new apn.Provider( 
    {
        token: {
            key: "*yourAuthKey.p8*",
            keyId: "*yourKeyId*",
            teamId: "*yourTeamId*"
        },
        production: false
    });

let notification = new apn.Notification();
notification.rawPayload = {
    "aps": {
        "alert": {
            "uuid": "*yourUuid*",
            "incoming_caller_id": "123456789",
            "incoming_caller_name": "Tester",
        }
    }
};
notification.pushType = "voip";
notification.topic = "*yourBundleId*.voip";


provider.send(notification, deviceToken).then((err, result) => {
    if (err) return console.log(JSON.stringify(err));
    return console.log(JSON.stringify(result))
});

See apn documentation for more informations.

Share:
2,227
Slaine06
Author by

Slaine06

Updated on December 24, 2022

Comments

  • Slaine06
    Slaine06 over 1 year

    Im developing a messaging application, which, right now uses FCM with Firebase functions to push notifications. For the VoIP service I am using Agora, but I want to add that native look to my application adding CallKit layouts when a user receive a call. Im using flutter_ios_voip_kit and I'm correctly getting the VoIP notification using the curl command in the terminal.

    curl -v \
    -d '{"aps":{"alert":{"uuid":"982cf533-7b1b-4cf6-a6e0-004aab68c503","incoming_caller_id":"0123456789","incoming_caller_name":"Tester"}}}' \
    -H "apns-push-type: voip" \
    -H "apns-expiration: 0" \
    -H "apns-priority: 0" \
    -H "apns-topic: <your app’s bundle ID>.voip" \
    --http2 \
    --cert ./voip_services.pem \
    https://api.sandbox.push.apple.com/3/device/<VoIP device Token for your iPhone>
    

    But I don't really know how to implement that request on my application. I want to keep it client side, so when a user tap on the call button I tried to run http.post call, but it does not work (I don't know if it's for a format mistake or something else).

    final url =
          'https://api.sandbox.push.apple.com:443/3/device/*myDeviceVoIPToken*';
      //final uuid = Uuid().v4();
      final Map body = {
        "aps": {
          "alert": {
            "uuid": "${Uuid().v4()}",
            "incoming_caller_id": "0123456789",
            "incoming_caller_name": "Tester"
          }
        }
      };
      Future<http.Response> postRequest() async {
        return await http.post(url,
            headers: <String, String>{
              'apns-push-type': 'voip',
              'apns-expiration': '0',
              'apns-priority': '0',
              'apns-topic': '*myBundleId*.voip'
            },
            body: jsonEncode(body));
    

    I also tried to use http2, but without luck. Can you give a code example of request VoIP notification using http.post? Or feel free to suggest better options. Thank you.

  • Slaine06
    Slaine06 over 3 years
    Thank you for your clear answer/explanation. Now I understand the need of pushing the notification from the server. But still I am a little bit weak back-end side. Do you know where can I find some example on how push VoIP notification from server? Do you know what are the best options?
  • Gabriel Gava
    Gabriel Gava over 3 years
    I must confess I'm not a backend guy too :D But I usually see people using the firebase environment to handle pushes on flutter apps. Please take a look on this link medium.com/@jun.chenying/… And for a more detailed explanation of how to implement it yourself, check the Apple docs on developer.apple.com/documentation/usernotifications/…
  • Slaine06
    Slaine06 over 3 years
    Thank you anyway, you answer helped me finding the right way. By the way I solved. I will leave my code snippets in the answer below!
  • Zionnite
    Zionnite about 2 years
    Please, how do can I get my device token??
  • Zionnite
    Zionnite about 2 years
    can you please share your full source code?