使用bark App实现向手机推送消息的流程

  • Post author:
  • Post category:其他




准备工作

  • bark是一个app,首先需要在手机上下一个bark app



第一步:部署bark-server服务

项目地址:https://github.com/Finb/bark-server

步骤:

  • 下载项目源码到本地
  • 进入项目目录
  • 编译安装,执行

    make install

    ,安装好的服务在dist目录
  • cd dist
  • 启动服务

    …/bark-server_darwin_amd64 -addr 0.0.0.0:8080 -data ./bark-data

完整步骤代码

java0904@weigongdeMacBook-Pro ~ % git clone https://github.com/Finb/bark-server.git
正克隆到 'bark-server'...
remote: Enumerating objects: 772, done.
remote: Counting objects: 100% (254/254), done.
remote: Compressing objects: 100% (67/67), done.
remote: Total 772 (delta 209), reused 187 (delta 187), pack-reused 518
接收对象中: 100% (772/772), 294.28 KiB | 975.00 KiB/s, 完成.
处理 delta 中: 100% (432/432), 完成.

java0904@weigongdeMacBook-Pro ~ % cd bark-server 

java0904@weigongdeMacBook-Pro bark-server % make
bash .cross_compile.sh bark-server
build => dist/bark-server_darwin_amd64
build => dist/bark-server_darwin_arm64
build => dist/bark-server_linux_386
build => dist/bark-server_linux_amd64
build => dist/bark-server_linux_arm
build => dist/bark-server_linux_arm64
build => dist/bark-server_windows_386.exe
build => dist/bark-server_windows_amd64.exe


java0904@weigongdeMacBook-Pro bark-server % cd dist 
java0904@weigongdeMacBook-Pro dist % ll
total 180640
drwxr-xr-x  10 java0904  staff       320  3  3 11:42 ./
drwxr-xr-x  25 java0904  staff       800  3  3 11:41 ../
-rwxr-xr-x   1 java0904  staff  12949424  3  3 11:41 bark-server_darwin_amd64*
-rwxr-xr-x   1 java0904  staff  12663666  3  3 11:41 bark-server_darwin_arm64*
-rwxr-xr-x   1 java0904  staff  10219520  3  3 11:41 bark-server_linux_386*
-rwxr-xr-x   1 java0904  staff  11993088  3  3 11:42 bark-server_linux_amd64*
-rwxr-xr-x   1 java0904  staff  10289152  3  3 11:42 bark-server_linux_arm*
-rwxr-xr-x   1 java0904  staff  11206656  3  3 11:42 bark-server_linux_arm64*
-rwxr-xr-x   1 java0904  staff  10705408  3  3 11:42 bark-server_windows_386.exe*
-rwxr-xr-x   1 java0904  staff  12455424  3  3 11:42 bark-server_windows_amd64.exe*

java0904@weigongdeMacBook-Pro dist % ./bark-server_darwin_amd64 -addr 0.0.0.0:8080 -data ./bark-data 

2022-03-03 11:47:57	INFO	init apns client success...
2022-03-03 11:47:57	INFO	load route [register_compat] success...
2022-03-03 11:47:57	INFO	load route [misc] success...
2022-03-03 11:47:57	INFO	load route [push] success...
2022-03-03 11:47:57	INFO	load route [register] success...
2022-03-03 11:47:57	INFO	load route [push_compat] success...
2022-03-03 11:47:57	INFO	init database [./bark-data]...
2022-03-03 11:47:57	INFO	Bark Server Listen at: 0.0.0.0:8080

 ┌───────────────────────────────────────────────────┐ 
 │                   Fiber v2.20.0                   │ 
 │                 http://[::]:8080                  │ 
 │                                                   │ 
 │ Handlers ............ 26  Processes ........... 1 │ 
 │ Prefork ....... Disabled  PID ............. 23986 │ 
 └───────────────────────────────────────────────────┘ 



第二步:在本地设置内网转发

使用的工具是:ngrok

地址是:https://dashboard.ngrok.com/get-started/setup

使用步骤:

  • 下载客户端 mac使用

    brew install ngrok/ngrok/ngrok
  • 登陆ngrok官网,获取authtoken,地址https://dashboard.ngrok.com/get-started/your-authtoken
  • 本地执行

    ngrok authtoken yourtoken
  • 启动服务

    ngrok http 8080

完整步骤:

java0904@weigongdeMacBook-Pro ~ % brew install ngrok/ngrok/ngrok
Running `brew update --preinstall`...
^C==> Tapping ngrok/ngrok
Cloning into '/usr/local/Homebrew/Library/Taps/ngrok/homebrew-ngrok'...
remote: Enumerating objects: 57, done.
remote: Counting objects: 100% (57/57), done.
remote: Compressing objects: 100% (36/36), done.
remote: Total 57 (delta 14), reused 51 (delta 13), pack-reused 0
Receiving objects: 100% (57/57), 6.77 KiB | 1.69 MiB/s, done.
Resolving deltas: 100% (14/14), done.
Tapped 1 cask (13 files, 13.4KB).
==> Downloading https://bin.equinox.io/a/egaWz4kj88z/ngrok-2.3.40-darwin-amd64.zip
######################################################################## 100.0%
==> Installing Cask ngrok
==> Linking Binary 'ngrok' to '/usr/local/bin/ngrok'
🍺  ngrok was successfully installed!
java0904@weigongdeMacBook-Pro ~ % ngrok authtoken 25rW************MB6
Authtoken saved to configuration file: /Users/java0904/.ngrok2/ngrok.yml
java0904@weigongdeMacBook-Pro ~ % ngrok http 8080




ngrok by @inconshreveable                                                                                                                                                                   (Ctrl+C to quit)
                                                                                                                                                                                                            
Session Status                online                                                                                                                                                                        
Account                       silk (Plan: Free)                                                                                                                                                             
Version                       2.3.40                                                                                                                                                                        
Region                        United States (us)                                                                                                                                                            
Web Interface                 http://127.0.0.1:4040                                                                                                                                                         
Forwarding                    http://99cb-xxx-xxx-xxx-243.ngrok.io -> http://localhost:8080                                                                                                                 
Forwarding                    https://99cb-xxx-xxx-xxx-243.ngrok.io -> http://localhost:8080                                                                                                                
                                                                                                                                                                                                            
Connections                   ttl     opn     rt1     rt5     p50     p90                                                                                                                                   
                              0       0       0.00    0.00    0.00    0.00                                                                                                                                  
                                                                                



第三步,在bark app上配置服务器的地址

在这里插入图片描述

这一步骤之后,在第一步部署的bark-server服务端,会出现请求记录,这个请求记录里面,有一个key值,就是这个设备的唯一标识,

这个key的值,在下一步需要用到。

2022-03-03 14:04:24     INFO    ::1 -> [200] GET 43.537054ms /register => /register?devicetoken=5535e5a2e9ec********714e1475700f&key=GK************Xe8



第四步 发送推送消息

参考官网文档:https://github.com/Finb/bark-server/blob/master/docs/API_V2.md

在这里插入图片描述

可以直接用curl命令发送,参考

curl -X "POST" "http://localhost:8080/push" \
     -H 'Content-Type: application/json; charset=utf-8' \
     -d $'{
  "body": "祝您生日快乐🌹🍰",
  "device_key": "这里就是用户的设备的key",
  "title": "这是标题",
  "badge": 1,
  "category": "category",
  "sound": "minuet.caf,这是用户收到推送消息后,手机发出的响声",
  "icon": "https://这里是图片的地址g/pic1.0dcc7dab.png",
  "group": "test",
  "url": "http://www.这是用户点击推送的消息后,跳转的地址"
}'



版权声明:本文为silk_java原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。