parnissolle parnissolle - 1 year ago 116
iOS Question

Open iOS app from background

I have an iOS app with Firebase Cloud Messaging to manage my push notifications. It works well, but I'd like to open my app (while in background state) when I receive my push notification. A bit like WhatsApp, with a custom calling (WebRTC) view.

Any ideas? Thanks.


Apparently I should use PushKit to do what I want to do.

Is there an other way to do that ... like

application. A ringing notification (push ?), and then user click on to open app.

Answer Source

Till FCM does not support silent push notification ( Push kit ).

Using Pushkit is only current technique to send silent push notification to device.

One you receive silent push notification payload, then you have to schedule local notification because silent push notification does not come in notification center. upto your local notification sound file plays ( Max 30 seconds ) your application will be invoke in background or kill state. you can do require activity here but not UI activity.

While tapping on interactive local notification you can do further UI activity.

Use below structure to achieve your task.

Use this simplepush.php file


// Put your device token here (without spaces):

      $deviceToken = '1234567890123456789';

// Put your private key's passphrase here:
$passphrase = 'ProjectName';

// Put your alert message here:
$message = 'My first push notification!';

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'PemFileName.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
//  'ssl://', $err,
    'ssl://', $err,

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body

$body['aps'] = array(
                     'content-available'=> 1,
                     'alert' => $message,
                     'sound' => 'default',
                     'badge' => 0,

// 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;
    echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server

Use below commands to create pem file and use it in above code

$ openssl x509 -in aps_development.cer -inform der -out PushCert.pem

# Convert .p12 to .pem. Enter your pass pharse which is the same pwd that you have given while creating the .p12 certificate. PEM pass phrase also same as .p12 cert.  
$ openssl pkcs12 -nocerts -out PushKey1.pem -in pushkey.p12

Enter Import Password:

MAC verified OK

Enter PEM pass phrase:

Verifying - Enter PEM pass phrase:

# To remove passpharse for the key to access globally. This only solved my stream_socket_client() & certificate capath warnings.
$ openssl rsa -in PushKey1.pem -out PushKey1_Rmv.pem

Enter pass phrase for PushChatKey1.pem:

writing RSA key

# To join the two .pem file into one file:
$ cat PushCert.pem PushKey1_Rmv.pem > ApnsDev.pem

After that go to simplepush.php location and fire command -> php simplepush.php

This way you can test your push kit notification setup architecture.


import UIKit
import PushKit

class AppDelegate: UIResponder, UIApplicationDelegate,PKPushRegistryDelegate{

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]

    self. PushKitRegistration()

    return true

//MARK: - PushKitRegistration

func PushKitRegistration()

    let mainQueue = dispatch_get_main_queue()
    // Create a push registry object
    if #available(iOS 8.0, *) {

        let voipRegistry: PKPushRegistry = PKPushRegistry(queue: mainQueue)

        // Set the registry's delegate to self

        voipRegistry.delegate = self

        // Set the push type to VoIP

        voipRegistry.desiredPushTypes = [PKPushTypeVoIP]

    } else {
        // Fallback on earlier versions


@available(iOS 8.0, *)
func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
    // Register VoIP push token (a property of PKPushCredentials) with server

    let hexString : String = UnsafeBufferPointer<UInt8>(start: UnsafePointer(credentials.token.bytes),
        count: credentials.token.length).map { String(format: "%02x", $0) }.joinWithSeparator("")



@available(iOS 8.0, *)
func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
    // Process the received push
    // From here you have to schedule your local notification



enter image description here

enter image description here

In parallel if you setup Socket architecture. Then Socket used to broadcast data for example which users are online / offline and you need to know without calling an API then you can use socket, socket architecture on server will send data on online / offline user to devices without making request from device.

Let me know if i could help you somewhere.

Have a happy coding.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download