【问题标题】:How to keep vibrate device in the background如何将振动设备保持在后台
【发布时间】:2016-11-22 17:33:45
【问题描述】:

当来电来电且应用程序位于前台时,我正在执行 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,则可以解决此问题,但是当静音开关打开时,此类别不会静音。

我看到其他应用程序像

Line

  • (在应用程序中接收传入)他们可以保持振动设备后 最小化直到达到呼叫超时(无人接听)约一分钟而停止前没有通知显示(接收呼叫结束通知)。
  • (在应用关闭时接收传入)他们也这样做,因为当我关闭通知时,设备仍然可以振动相同的持续时间。

Facebook Messenger

  • (在应用程序中接收传入)它们会显示长通知以保持设备振动。如果我关闭该通知,它将立即停止。

  • (在应用关闭时接收传入)如果我关闭通知,设备会再振动 2-3 次并显示较长的通知(与在应用中接收传入时相同)

Viber, Skype

  • (在应用程序中接收传入)我不确定Skype,但对于viber,当最小化应用程序时它会立即停止振动。

  • (应用关闭时接收传入)我认为他们的做法与 facebook 相同(不确定)

如何在最小化应用程序时让设备在不通知Line的情况下振动?

如果我设法找到一种方法并使其像Line 一样工作,我是否需要在应用程序不在前台时使用 Voip 推送来实现该方法? (正常的推送通知会起作用吗?)

【问题讨论】:

  • 你使用推送通知还是 Pushkit(静默推送通知)?
  • 有效吗?
  • @Hasya 抱歉,还没有时间研究。我设法让它在没有PushKit 的情况下工作Receive incoming in the app(最小化时保持振动设备)。但是对于在应用程序之外时,我认为PushKit 是必需的。将遵循您的建议并在周末提供反馈。
  • 别担心,编码愉快。让我知道有关基于 VOIP 的应用程序的任何帮助。

标签: ios voip iphone-vibrate


【解决方案1】:

您必须使用 pushkit(静默推送通知)。

用于后台或终止状态的应用程序。

Skype、whatsapp、viber 等基于 VOIP 的应用程序的工作方式类似于以下架构。

当您获得有效负载时,您必须安排本地通知。您可以在本地通知中设置声音文件。该声音文件最多播放 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://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

}

}

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

【讨论】:

  • 我让它工作并添加一个答案,包括现在如何在后台振动设备。谢谢。
【解决方案2】:

经过一些测试,我找到了Line 的方法。

在应用中接听来电

他们使用

[[UIApplication sharedApplication] beginBackgroundTaskWithName:expirationHandler:]

即使您已经将其最小化,也可以保持应用振动。此方法将保持应用运行,并在过期前有 180 秒的持续时间。

应用关闭时接听来电

这需要使用PushKit 作为@Hasya 提及使应用程序唤醒并调用与上述场景相同的方法(保持设备振动)

[[UIApplication sharedApplication] beginBackgroundTaskWithName:expirationHandler:]

这一次它将在 40 秒而不是 180 秒内到期。您也不能通过AVAudioPlayer 和Ambient 类别在这里播放传入的声音。

所以你需要在UILocalNotification 上附加一个声音(需要一个扩展版本,因为你没有重复功能),我在某处读过它,你可以播放长达 30 秒的声音。

使用PushKit时的一些注意事项

如果您有使用 APNS 的旧系统,您需要在服务器端维护两个令牌(APNS 和 VOIP)。因为您无法使用 VOIP 证书发送 APNS 令牌。令牌需要与正确的证书类型一起发送。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 2019-07-30
    • 1970-01-01
    相关资源
    最近更新 更多