2016-09-16 106 views
1

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

有没有人有一个想法他们是如何实现的,快速接收一堆消息?

+0

你实现通过使用套接字或xmpp服务器 – Monish

+0

我使用套接字 – Branko

+0

实际上你需要实现机制来批量获取消息。如果你观察到你在whatsapp上得到这么多的消息,那么就会批量获取消息。 – Monish

回答

1

你应该使用PushKit,在您的Xcode项目>功能面板中启用IP语音背景模式一起。这样,VoIP推送通知可以唤醒您的应用程序,因此您可以处理收到的数据并显示它。与标准推送通知相比,VoIP推送还有几个优势。更多信息可以在here找到。

+0

是的。使用pushkit @Branko可以像上面提到的那样管理架构。 – Hasya

0

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

使用下面的结构来实现您的任务。

使用下面

<?php 

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


     $deviceToken = '123456789'; 
// 


// 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); 

使用此simplepush.php文件命令创建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 

} 

} 

让我知道如果你需要更多的帮助。 祝您有个愉快的编码。