2016-11-22 21 views
3

着信があり、アプリがフォアグラウンドになったら、私はVOIPアプリをやっています。私は下のコードでリンガーを演奏するバックグラウンドでデバイスを振動させ続ける方法

self.ringTimer = [[TimerService sharedInstance] createIntervalTimerWithInterval:2500 queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) andBlock:^{ 
    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); 
}]; 

NSString *ringingPath = [[NSBundle mainBundle] pathForResource:@"incoming_sound" ofType:@"mp3"]; 

AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
[audioSession setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionMixWithOthers error:nil]; 

// for normal phone receiver route it to speaker 
if ([[audioSession audioRoute] isEqualToString:kAudioSessionDevice_Phone]) { 
    [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil]; 
} 

NSURL *soundUrl = [NSURL fileURLWithPath:ringingPath]; 
NSError *error; 
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:&error]; 

self.audioPlayer.numberOfLoops = -1; 
[self.audioPlayer play]; 

上記のコードはうまく動作し、サウンドを再生するときにはサイレント切り替えボタンも尊重します。

しかし、私がアプリを最小限に抑えると、デバイスは振動を止め、入ってくるサウンドを再生します。これは、AVAudioSessionカテゴリをAVAudioSessionCategoryPlaybackに設定すると克服できますが、このカテゴリはサイレントスイッチがオンのときには音を消音しません。

私は約1

Line

  • のような他のアプリを(アプリでの着信受信)は、コールのタイムアウトに達するまで、(誰も答え)を最小化しない 後バイブレーションデバイスを保つことができます参照してください(着信終了通知)を通知することなく、その旨を通知する。
  • (アプリが終了したときに受信を受信する)も同じです。通知を却下すると、デバイスは同じ期間、引き続き振動する可能性があります。 Facebook Messenger

  • は、彼らは、デバイスが振動を維持するために長い通知を表示(アプリでの着信受信します)。その通知を却下するとすぐに停止します。

  • (アプリを閉じたときに、着信受信)私は、通知、2~3回以上のデバイスが振動を却下し、(アプリでの着信受信したときと同じ)の長通知

Viberを示す場合、Skype

  • それが瞬時にバイブレーションを停止アプリを最小化したとき(アプリでの着信受信)私は、Skypeのための、しかし、Viberのためにわかりません。

  • (アプリを閉じたときに、着信受信)私は、彼らがFacebookの(ないように注意してください)と同じ方法を行っていると思うアプリを最小化したとき

がどのように私はLineよう通知することなく、デバイスを振動させることができますか?

私が方法を見つけてそれをLineのように動作させると、その方法を実装するためにアプリがフォアグラウンドにないときにVoipプッシュを使用する必要がありますか? (通常のプッシュ通知は動作しますか?)

+0

プッシュ通知またはプッシュキット(サイレントプッシュ通知)を使用していますか? – Hasya

+0

それは働いたのですか? – Hasya

+0

@Hasya申し訳ありませんが、まだそれを調べる時間がありません。私はそれを 'PushKit'なしで動作させるように管理しています。「アプリケーションで受信する」(最小化すると、デバイスを振動させ続ける)。しかし、アプリの外では、私は 'PushKit'が必要だと思う。あなたの提案に従い、週末にフィードバックを与えます。 – SaintTail

答えて

2

プッシュキット(サイレントプッシュ通知)を使用する必要があります。

バックグラウンドまたは強制終了状態でのアプリケーションの場合。

Skype、whatsapp、viberなどVOIPベースのアプリは以下のアーキテクチャのように動作します。

ペイロードが取得されたら、ローカル通知をスケジュールする必要があります。ローカル通知でサウンドファイルを設定することができます。そのサウンドファイルは最大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ファイルは、場所や火のコマンドを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 

でそれを使用するコマンド - > php simplepush.php

この方法で、プッシュキットの通知設定アーキテクチャをテストできます。並行して

https://zeropush.com/guide/guide-to-pushkit-and-voip

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 

} 

} 

あなたのセットアップソケットアーキテクチャの場合。その後、ソケットは、ユーザーがオンライン/オフラインでデータをブロードキャストするために使用され、APIを呼び出すことなくソケットを使用することができます。サーバー上のソケットアーキテクチャは、デバイスからの要求を行わずにオンライン/オフラインユーザーのデータをデバイスに送信します。

どこかお手伝いできるかどうか教えてください。

幸いなコーディングがあります。

+0

私はそれを動作させ、バックグラウンドでデバイスを振動させる方法を含む回答を追加しました。ありがとう。 – SaintTail

0

いくつかのテストの後、私は方法を見つけましたLineそれをしました。

は、彼らはあなたがすでにそれを最小化しているにもかかわらず、アプリが振動を維持するために

[[UIApplication sharedApplication] beginBackgroundTaskWithName:expirationHandler:]

を使用するアプリで

を着信コールを受信します。このメソッドは、アプリケーションを実行したままにし、有効期限が切れるまでの時間が180秒になります。

アプリは

アプリが起き作るために@Hasya言及としてPushKitを使用してもシナリオ上記と同様の方法を呼び出すために、この必要性を閉じているとき

[[UIApplication sharedApplication] beginBackgroundTaskWithName:expirationHandler:]

(デバイスのバイブレーションを保つ)着信コールを受信

今回は、180秒ではなく40で期限が切れます。また、AVAudioPlayerで受信音を再生することはできません。Ambientカテゴリはここにあります。

あなたは30秒までサウンドを再生できるどこかで読んだUILocalNotificationにサウンド(リピート機能がないので拡張バージョンが必要です)を添付する必要があります。

あなたは、サーバー側の両方のトークン(APNSとVOIP)を維持する必要がAPNS使用古いシステムを使用PushKit

を持っている場合はいくつかの懸念。 VOIP証明書付きのAPNSトークンを送信できないためです。トークンは、正しい証明書のタイプで送信する必要があります。

関連する問題