【问题标题】:Push notifications instant message receiving like on Whatsapp or Viber在 Whatsapp 或 Viber 上接收推送通知即时消息
【发布时间】:2016-09-16 09:24:13
【问题描述】:

当 Whatsapp(或 Viber)iOS 应用程序完全关闭(从后台删除)时,消息接收将按预期通过推送通知进行处理。
当您从推送通知中唤醒 Whatsapp(或 Viber)时,它会立即显示所有收到的消息,而无需明确地从服务器下载它们。
实际上,看起来每个推送通知都会唤醒应用程序有足够的时间来下载已发送的消息,我相信这在 iOS 中是不可能的。

有人知道他们是如何实现快速接收大量消息的吗?

【问题讨论】:

  • 你通过使用socket或者xmpp服务器实现
  • 其实需要实现批量获取消息的机制。如果您观察到当您在whatsapp 上收到这么多消息时,那么您已经批量获取消息。
  • 我认为,如果我收到一批 od 10 条消息(假设每条 50kb)或一条一条消息,这没什么大不了的。最后,需要接收相同数量的数据,下载过程使一切变得缓慢。
  • 假设您在服务器上有 1000 条新消息。您已批量获取 100-100 条消息并存储在本地数据库中。现在你已经获取了所有 1000 条消息并存储在本地数据库中,所以有时你的 UI 会被冻结。
  • 没错,但问题是在 whatsapp 上,看起来消息是立即下载的。我只是从推送通知中打开应用程序,它们就在那里。批处理解决方案不会加快消息下载速度,这实际上是我正在尝试解决的问题

标签: ios push-notification whatsapp viber


【解决方案1】:

您可能应该使用 PushKit,以及在 Xcode Project > Capabilities 窗格中启用的 Voice over IP 背景模式。这样 VoIP 推送通知可以唤醒您的应用程序,因此您可以处理接收到的数据并显示它。与标准推送通知相比,VoIP 推送也有几个优点。更多信息可以在here找到。

【讨论】:

  • 是的。使用 pushkit @Branko,您可以像上面提到的那样管理架构。
【解决方案2】:

您的应用会在后台调用,无论它处于后台模式还是终止模式。您的应用将在本地通知播放(最长 30 秒)之前处于活动状态。

使用下面的结构来完成你的任务。

使用这个 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

}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 2018-10-26
    • 1970-01-01
    相关资源
    最近更新 更多