【swift】プッシュ通知 iOS10

バイストークン取得&通知

class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool{
if (UIDevice.current.systemVersion as NSString).floatValue >= 8.0 {
let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
print(deviceTokenString)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("error: \(error)")
}

pemファイルの作り方

openssl pkcs12 -in aps_development.p12 -out aps_development.pem -nodes -clcerts

entrust_root_certification_authority.pem ファイルの作り方

wget https://www.entrust.net/downloads/binary/entrust_2048_ca.cer -O - > entrust_root_certification_authority.pem


PHPソースコード

<?php

$msg = "";
if(!isset($_GET["msg"])){
return;
}
$msg = $_GET["msg"];
date_default_timezone_set('Asia/Tokyo');
error_reporting(-1);
require_once 'ApnsPHP/Autoload.php';
$push = new ApnsPHP_Push(
ApnsPHP_Abstract::ENVIRONMENT_SANDBOX,
'aps_development.pem'
);
$push->setRootCertificationAuthority('entrust_root_certification_authority.pem');
$push->connect();
$message = new ApnsPHP_Message('デバイストークン');
$message->setCustomIdentifier("Message-Badge-3");
$message->setBadge(1);
$message->setText($msg);
$message->setSound();
//$message->setCustomProperty('acme2', array('bang', 'whiz'));
//$message->setCustomProperty('acme3', array('bing', 'bong'));
$message->setExpiry(30);
$push->add($message);
$push->send();
$push->disconnect();
$aErrorQueue = $push->getErrors();
if (!empty($aErrorQueue)) {
var_dump($aErrorQueue);
}

参考サイト

返信を残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

CAPTCHA