在iOS中静音远程通知后响铃

时间:2016-10-06 10:14:48

标签: ios objective-c apple-push-notifications

我有iOS的静音远程通知,如下所示:

{
    myVar1 = myVal1;
    aps =     {
        "content-available" = 1;
    };
    myVar2 = myVal2;
}

我进去了:

- (void) application:(UIApplication*)application didReceiveRemoteNotification:(nonnull NSDictionary *)myParams fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
    if ([ myParams isEqualToString:@"myVal1"])  
    //send localnotification and play sound 
}  

根据myVar1,当iOS中的应用程序处于后台模式时,我会采取不同的操作。

我希望能够播放自定义铃声。我所做的是以下内容:

UILocalNotification *localNotification=[[UILocalNotification alloc] init];
localNotification.fireDate=[NSDate dateWithTimeIntervalSinceNow:0];
localNotification.alertBody=@"Some message";
localNotification.timeZone=[NSTimeZone defaultTimeZone];
localNotification.soundName=@"mySound";
localNotification.alertTitle=@"Some title";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSInteger numberOfBadges = [UIApplication sharedApplication].applicationIconBadgeNumber+1;
localNotification.applicationIconBadgeNumber = numberOfBadges;



[[UIApplication sharedApplication] setApplicationIconBadgeNumber:numberOfBadges];

对于localNotification.soundName = @" mySound&#34 ;;

我尝试了以下内容:

localNotification.soundName=@"mySound.wav";
localNotification.soundName=@"mySound.m4r";
localNotification.soundName=[NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] @"mySound.m4r"];
localNotification.soundName=[NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] @"mySound.wav"];

虽然我收到了本地通知,但我可以在这里找到wav文件(铃声),只是iOS的默认提示音。

有什么不对吗?

由于

1 个答案:

答案 0 :(得分:0)

检查,如果您的声音文件超过30秒,则无法播放。

您是否实施了类似VOIP的通话?那么我相信你必须使用PushKit。

您可以维护套接字体系结构,以便在线或离线时继续广播哪个用户。

即使您可以将本地通知对象保留在NSUSderDefault中,因此即使您的设备重新启动,您也可以在NSUSderDefault

检索didFinishLaunchingWithOption对象

当您的应用是关于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位置和fire命令 - &gt; 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