笔记 iOS消息推送 测试代码

1.平常开发通常使用第三方推送, 这里自己整理记录不使用第三方推送的操作流程。

2.登陆 https://developer.apple.com,  在Identifiers App IDs 下, 找到需要推送的app,  找到推送选项, 勾选, 上传文件, 生成证书,下载并导入钥匙链。

3.在钥匙链中找到我们下载的证书,

4.接下里竞选中Apple  xxxx Services 导出, 格式为.p12文件给自己服务器。 至此, 证书已经导出完成。

5.xcode 设置:


framework导入, 需要注意UserNotifications, 如果在编译报错, 适配iOS8以下版本, 这里要选optional.

6.代码,请求推送权限

UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

获取token, 上传服务器,

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    //获取token , 上传服务器
}

7,客户端单独测试。这里使用php 代码进行测试, 因为mac 自带。

导出证书,并且命名为aps_dev.cer

同样操作, 导出.p12文件。设置密码123456

接下来, 打开控制台, cd 到存储目录 .

执行以下操作:

a. openssl x509 -in aps_dev.cer -inform der -out PushChatCert.pem

b.  openssl pkcs12 -nocerts -out PushChatKey.pem -in push.p12

c.  cat PushChatCert.pem PushChatKey.pem > ck.pem

至此, 文件已经制作完毕,打开文本编辑器。 输入php 代码, 并执行, 即可

<?php

// Put your device token here (without spaces):
$deviceToken = '450f094a8c9032189ab06d3e26725d247150d5fa71520c6ee5cf1641d54ccc34 ';

// Put your private key's passphrase here:密语
$passphrase = '123456';

// Put your alert message here:
$message = '820fc655ec5d2ead8d37889689e0c8f078a6d236a40e25d9d80072f88125ec1c';



$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
stream_context_set_option($ctx, 'ssl', 'verify_peer', false);

// Open a connection to the APNS server
$fp = stream_socket_client(
	'ssl://gateway.sandbox.push.apple.com:2195', $err,
	$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
// $fp = stream_socket_client(
// 	'ssl://gateway.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(
	'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);
    
?>

文件中写入的相对路径, php 要放在同一个目录下面。

网上有些推送信息太老, 有点不适用了。在此记录方便后面使用。








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js是一种流行的JavaScript框架,用于构建用户界面。它提供了一种简洁、灵活的方式来实现前端开发。WebSocket是一种在Web浏览器和服务器之间进行全双工通信的协议,可以实现实时消息推送。 在Vue.js中实现WebSocket消息推送可以通过以下步骤: 1. 首先,安装WebSocket库。可以使用npm或yarn来安装,例如:`npm install vue-native-websocket`。 2. 在Vue.js的入口文件(通常是main.js)中导入WebSocket库,并配置WebSocket连接。示例代码如下: ```javascript import VueNativeSock from 'vue-native-websocket'; Vue.use(VueNativeSock, 'ws://localhost:8080', { format: 'json', reconnection: true, reconnectionAttempts: 5, reconnectionDelay: 3000, }); ``` 上述代码中,我们使用了`vue-native-websocket`库,并配置了WebSocket连接的URL、数据格式以及重连选项。 3. 在Vue组件中使用WebSocket。可以通过在组件中添加`this.$socket`来访问WebSocket实例,并监听相应的事件。例如: ```javascript export default { mounted() { this.$socket.addEventListener('message', this.handleMessage); }, methods: { handleMessage(event) { // 处理接收到的消息 }, sendMessage(message) { this.$socket.send(message); }, }, }; ``` 上述代码中,我们在组件的`mounted`钩子函数中监听了`message`事件,并定义了处理接收到消息的方法`handleMessage`。同时,我们还定义了一个`sendMessage`方法来发送消息。 这样,当WebSocket连接建立后,就可以通过`this.$socket.send()`方法发送消息,并通过`this.$socket.addEventListener()`方法监听接收到的消息

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值