ios - 使用通知从后台唤醒 iOS 应用程序

标签 ios swift firebase webrtc firebase-notifications

我有一个带有 Firebase 云消息传递的 iOS 应用程序来管理我的推送通知。它运行良好,但我想在收到推送通知时打开我的应用程序(在后台状态下)。有点像 WhatsApp,具有自定义调用 (WebRTC) View 。

有什么想法吗?

显然我应该使用 PushKit 来做我想做的事。

有没有其他方法可以做到这一点...比如 Signal 应用程序。响铃通知(按?),然后用户点击打开应用。

最佳答案

直到 FCM 不支持静默推送通知( Push kit )。

使用 Pushkit 只是目前向设备发送静默推送通知的技术。

如果您收到静默推送通知负载,那么您必须安排本地通知,因为静默推送通知不会进入通知中心。直到您的本地通知声音文件播放(最多 30 秒),您的应用程序将在后台或终止状态下调用。您可以在此处要求事件,但不能要求 UI 事件。

点击交互式本地通知时,您可以进行进一步的 UI 事件。

使用以下结构来完成您的任务。

使用这个 simplepush.php 文件

<?php

// 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://gateway.push.apple.com:2195', $err,
    '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(
                     '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;
else
    echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);

使用下面的命令创建pem文件并在上面的代码中使用它

$ 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

之后转到 simplepush.php 位置并启动命令 -> php simplepush.php

这样您就可以测试您的推送套件通知设置架构。

https://www.raywenderlich.com/123862/push-notifications-tutorial

Download

import UIKit
import PushKit


class AppDelegate: UIResponder, UIApplicationDelegate,PKPushRegistryDelegate{



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


    let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]
    application.registerForRemoteNotificationTypes(types)

    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("")

    print(hexString)


}


@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

如果您设置套接字架构,则并行。然后 Socket 用于广播数据,例如哪些用户在线/离线,你需要知道而不调用 API 然后你可以使用套接字,服务器上的套接字架构将在线/离线用户的数据发送到设备而不向设备发出请求。

关于ios - 使用通知从后台唤醒 iOS 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39896762/

相关文章:

iphone - UISegmentedControl 在 UIView 中没有响应

ios - 滚动时如何将 SearchBar 固定为 UITableView 的标题?

ios - 将图像写入目录中的文件

java - Firebase - 在类上找不到要序列化的属性

java - 从 Firebase 存储中获取随机图像

angular - firebase.firestore.FieldValue.serverTimestamp() 不支持时间戳数据类型

ios - 根据 Apple 的 Provisioning 文档,什么是团队分销商?

c++ - Cocos2Dx "use of undeclared identifier"iOS游戏内购

ios - 应用更新 CoreData 保存了吗?

macos - 滚动时 NSTableView 非常滞后