Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
app_push
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ikcrm_server
app_push
Commits
4af508aa
Commit
4af508aa
authored
May 16, 2019
by
zmj
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
集成华为推送
parent
e4fb872f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
102 additions
and
5 deletions
+102
-5
lib/app_push.rb
+10
-4
lib/app_push/huawei.rb
+91
-0
lib/app_push/version.rb
+1
-1
No files found.
lib/app_push.rb
View file @
4af508aa
...
...
@@ -4,6 +4,7 @@ require 'app_push/igetui'
require
'app_push/igetui/push'
require
'app_push/log'
require
'igetui'
require
'app_push/huawei'
module
AppPush
class
Push
class
<<
self
...
...
@@ -27,6 +28,10 @@ module AppPush
# }
# }
@igetui_config
=
opts
[
'igetui'
]
if
@igetui_config
.
present?
Igetui
.
config
=
@igetui_config
[
'opts'
]
Igetui
::
Push
.
config
=
@igetui_config
[
'base'
]
end
# xiaomi default config
# {
# restricted_package_name: 'com.vcooline.aike',
...
...
@@ -36,11 +41,11 @@ module AppPush
# notify_type: 1
# }
@xiaomi_config
=
opts
[
'xiaomi'
]
if
@igetui_config
.
present?
Igetui
.
config
=
@igetui_config
[
'opts'
]
Igetui
::
Push
.
config
=
@igetui_config
[
'base'
]
end
Xiaomi
.
config
=
@xiaomi_config
# huawei default config
@huawei_config
=
opts
[
'huawei'
]
Huawei
.
config
=
@huawei_config
end
# transmission_content 是真正的推送信息
...
...
@@ -58,6 +63,7 @@ module AppPush
Xiaomi
.
push
(
user_device_ids
,
message
[
'transmission_content'
])
# huawei
Huawei
.
push
(
user_device_ids
,
message
[
'transmission_content'
])
# etc
rescue
StandardError
=>
err
...
...
lib/app_push/huawei.rb
0 → 100644
View file @
4af508aa
module
AppPush
module
Huawei
class
<<
self
attr_accessor
:config
,
:access_token
,
:expire_time
def
config
@config
=
{}
if
@config
.
blank?
@config
.
merge!
(
default_config
)
end
def
default_config
{
grant_type:
'client_credentials'
}
end
def
push
(
device_tokens
,
message
)
handle_token_res
if
access_token
.
blank?
||
(
expire_time
.
present?
&&
Time
.
now
>=
expire_time
)
device_tokens
.
each_slice
(
100
)
do
|
device_token_array
|
res_push
(
message
,
device_token_array
)
end
rescue
StandardError
=>
err
AppPush
::
Log
.
error
(
err
)
end
def
res_push
(
message
,
device_tokens
)
body
=
message
(
message
,
device_tokens
)
headers
=
{
'Content-Type'
=>
'application/x-www-form-urlencoded'
}
nsp_ctx
=
{
ver:
'1'
,
appId:
config
[
'app_id'
]
}
url
=
"https://api.push.hicloud.com/pushsend.do?nsp_ctx=
#{
URI
.
escape
(
nsp_ctx
.
to_s
)
}
"
AppPush
::
Log
.
info
(
"url:
#{
url
}
"
)
AppPush
::
Log
.
info
(
"body:
#{
body
}
"
)
response
=
HTTParty
.
post
(
url
,
body:
body
,
headers:
headers
,
timeout:
5
)
AppPush
::
Log
.
info
(
"response:
#{
response
}
"
)
response
end
# 推送信息
def
message
(
message
,
device_tokens
)
payload
=
{
hps:
{
msg:
{
type:
1
,
body:
message
}
}
}
{
access_token:
'access_token'
,
nsp_svc:
'openpush.message.api.send'
,
nsp_ts:
Time
.
now
.
to_i
,
device_token_list:
device_tokens
,
# 单次最多100
payload:
payload
}
end
def
handle_token_res
loop
do
res
=
req_token
if
res
[
'error'
].
blank?
@access_token
=
res
[
'access_token'
]
break
if
@access_token
.
present?
end
end
@expire_time
=
Time
.
now
+
58
.
minutes
end
def
req_token
url
=
'https://login.cloud.huawei.com/oauth2/v2/token'
body
=
{
grant_type:
'client_credentials'
,
client_id:
config
[
'app_id'
],
client_secret:
config
[
'app_secret'
]
}
headers
=
{
'Content-Type'
=>
'application/x-www-form-urlencoded'
}
AppPush
::
Log
.
info
(
"url:
#{
url
}
"
)
AppPush
::
Log
.
info
(
"body:
#{
body
}
"
)
response
=
HTTParty
.
post
(
url
,
body:
body
,
headers:
headers
,
timeout:
5
)
AppPush
::
Log
.
info
(
"response:
#{
response
}
"
)
JSON
.
parse
(
response
)
end
end
end
end
lib/app_push/version.rb
View file @
4af508aa
module
AppPush
VERSION
=
"0.1
1
"
VERSION
=
"0.1
2
"
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment