Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
L
LuckyFarm
Project
Project
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
王雪伟
LuckyFarm
Commits
464b75dd
Commit
464b75dd
authored
Nov 04, 2020
by
wangxuewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[提交人]:王雪伟
[提交简述] :多多菜园 [实现方案] :通知栏
parent
d8666875
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
287 additions
and
7 deletions
+287
-7
AndroidManifest.xml
app/src/main/AndroidManifest.xml
+2
-7
GameActivity.kt
app/src/main/java/com/ym/game/GameActivity.kt
+22
-0
NotificationService.java
app/src/main/java/com/ym/game/notif/NotificationService.java
+239
-0
notification_view.xml
app/src/main/res/layout/notification_view.xml
+24
-0
notif_bg.png
app/src/main/res/mipmap-xhdpi/notif_bg.png
+0
-0
notif_btn.png
app/src/main/res/mipmap-xhdpi/notif_btn.png
+0
-0
notif_icon.png
app/src/main/res/mipmap-xhdpi/notif_icon.png
+0
-0
No files found.
app/src/main/AndroidManifest.xml
View file @
464b75dd
...
...
@@ -8,7 +8,7 @@
<uses-permission
android:name=
"android.permission.ACCESS_COARSE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.ACCESS_FINE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.READ_PRIVILEGED_PHONE_STATE"
/>
<uses-permission
android:name=
"android.permission.EXPAND_STATUS_BAR"
/>
<uses-permission
android:name=
"android.permission.ACCESS_NETWORK_STATE"
/>
<uses-permission
android:name=
"android.permission.ACCESS_WIFI_STATE"
/>
...
...
@@ -45,13 +45,8 @@
<category
android:name=
"android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<service
android:name=
"com.ym.game.notif.NotificationService"
/>
<activity
android:name=
"com.ym.game.view.HomeWareActivity"
android:exported=
"true"
/>
<activity
android:name=
"com.ym.game.view.OrderListActivity"
android:exported=
"true"
/>
<activity
android:name=
"com.ym.game.activity.WxLoginActivity"
android:exported=
"true"
/>
...
...
app/src/main/java/com/ym/game/GameActivity.kt
View file @
464b75dd
package
com.ym.game
import
android.annotation.SuppressLint
import
android.content.ComponentName
import
android.content.Intent
import
android.content.ServiceConnection
import
android.os.Bundle
import
android.os.IBinder
import
android.text.TextUtils
import
android.util.Log
import
com.bx.adsdk.AdSdk
...
...
@@ -12,6 +15,7 @@ import com.ym.game.activity.WxLoginActivity
import
com.ym.game.listener.IDialogViewCloseCallback
import
com.ym.game.module.*
import
com.ym.game.net.GameApiClient
import
com.ym.game.notif.NotificationService
import
com.ym.game.view.*
import
com.ym.library.AppliContext
import
com.ym.library.Constant
...
...
@@ -75,9 +79,11 @@ class GameActivity : UnityPlayerActivity(), IDialogViewCloseCallback {
AdSdk
.
exposure
(
"2773"
,
SettingPreference
.
getToken
())
Constant
.
isReportXiaoman
=
false
}
openNotif
()
}
override
fun
onDestroy
()
{
scc
?.
let
{
unbindService
(
it
)
}
super
.
onDestroy
()
if
(
mRefreshLoginObservable
!=
null
)
{
RxBus
.
get
().
unregister
(
"refresh_login"
,
mRefreshLoginObservable
!!
)
//页面销毁的时候要执行 反注册
...
...
@@ -921,4 +927,20 @@ class GameActivity : UnityPlayerActivity(), IDialogViewCloseCallback {
}
private
var
myNotifService
:
NotificationService
?
=
null
private
var
scc
:
ServiceConnection
?
=
null
//打开通知栏
private
fun
openNotif
()
{
val
intent
=
Intent
(
this
,
NotificationService
::
class
.
java
)
scc
=
object
:
ServiceConnection
{
override
fun
onServiceConnected
(
name
:
ComponentName
,
service
:
IBinder
)
{
val
myBinder
:
NotificationService
.
MyBinder
=
service
as
NotificationService
.
MyBinder
myNotifService
=
myBinder
.
getService
()
}
override
fun
onServiceDisconnected
(
name
:
ComponentName
)
{}
}
bindService
(
intent
,
scc
as
ServiceConnection
,
BIND_AUTO_CREATE
)
}
}
\ No newline at end of file
app/src/main/java/com/ym/game/notif/NotificationService.java
0 → 100644
View file @
464b75dd
package
com
.
ym
.
game
.
notif
;
import
android.annotation.SuppressLint
;
import
android.app.Activity
;
import
android.app.Notification
;
import
android.app.NotificationChannel
;
import
android.app.NotificationManager
;
import
android.app.PendingIntent
;
import
android.app.Service
;
import
android.content.BroadcastReceiver
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.content.IntentFilter
;
import
android.os.Binder
;
import
android.os.Build
;
import
android.os.IBinder
;
import
android.widget.RemoteViews
;
import
androidx.core.app.NotificationCompat
;
import
com.ym.ddcy.R
;
import
com.ym.game.GameActivity
;
import
com.ym.library.utils.LogUtils
;
import
java.lang.reflect.Method
;
public
class
NotificationService
extends
Service
{
private
Context
context
;
private
RemoteViews
NormalView
;
private
Notification
notification
;
public
final
static
int
ID_FOR_CUSTOM_VIEW
=
7
;
private
ActionReceiver
receiver
=
new
ActionReceiver
();
public
final
static
String
NOTIFICATION_GET_FRUIT
=
"com.smile.notification.getfruit"
;
private
static
String
TAG
=
"NotificationService"
;
private
NotificationManager
mNotificationManager
;
public
class
MyBinder
extends
Binder
{
public
NotificationService
getService
()
{
return
NotificationService
.
this
;
}
}
//通过binder实现调用者client与Service之间的通信
private
MyBinder
binder
=
new
MyBinder
();
@Override
public
IBinder
onBind
(
Intent
intent
)
{
return
binder
;
}
@Override
public
boolean
onUnbind
(
Intent
intent
)
{
return
super
.
onUnbind
(
intent
);
}
public
static
void
startNotificationService
(
Context
ctx
)
{
try
{
// Boolean isOpen = (Boolean) SPUtils.get(ctx, SPUtils.SP_LONG_NOTICATION, true);
// if (!isOpen) {
// return;
// }
//开启常驻通知栏
Intent
intent3
=
new
Intent
(
ctx
,
NotificationService
.
class
);
ctx
.
startService
(
intent3
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
@Override
public
void
onCreate
()
{
super
.
onCreate
();
context
=
getApplicationContext
();
initNotification
();
IntentFilter
filter
=
new
IntentFilter
();
filter
.
addAction
(
NOTIFICATION_GET_FRUIT
);
registerReceiver
(
receiver
,
filter
);
}
@Override
public
int
onStartCommand
(
Intent
intent
,
int
flags
,
int
startId
)
{
updateNotivView
();
if
(
notification
!=
null
)
{
mNotificationManager
.
notify
(
ID_FOR_CUSTOM_VIEW
,
notification
);
}
return
Service
.
START_STICKY
;
}
@Override
public
void
onDestroy
()
{
super
.
onDestroy
();
if
(
receiver
!=
null
)
unregisterReceiver
(
receiver
);
}
private
void
initNotification
()
{
if
(
mNotificationManager
==
null
)
{
mNotificationManager
=
(
NotificationManager
)
getSystemService
(
Context
.
NOTIFICATION_SERVICE
);
}
PendingIntent
getFruitIntent
=
PendingIntent
.
getBroadcast
(
context
,
(
int
)
System
.
currentTimeMillis
(),
new
Intent
(
NOTIFICATION_GET_FRUIT
),
PendingIntent
.
FLAG_UPDATE_CURRENT
);
NotificationCompat
.
Builder
builder
=
new
NotificationCompat
.
Builder
(
context
,
createNotificationChannel
(
this
));
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
N
)
{
builder
.
setSmallIcon
(
R
.
mipmap
.
icon_launch
)
.
setContentTitle
(
context
.
getString
(
R
.
string
.
app_name
))
.
setContentText
(
context
.
getString
(
R
.
string
.
app_name
))
.
setTicker
(
context
.
getString
(
R
.
string
.
app_name
))
.
setPriority
(
Notification
.
PRIORITY_HIGH
)
.
setWhen
(
System
.
currentTimeMillis
())
.
setGroupSummary
(
true
)
.
setGroup
(
"group"
)
.
setOngoing
(
true
);
}
else
{
builder
.
setSmallIcon
(
R
.
mipmap
.
icon_launch
)
.
setContentTitle
(
context
.
getString
(
R
.
string
.
app_name
))
.
setContentText
(
context
.
getString
(
R
.
string
.
app_name
))
.
setTicker
(
context
.
getString
(
R
.
string
.
app_name
))
.
setPriority
(
Notification
.
PRIORITY_HIGH
)
.
setWhen
(
System
.
currentTimeMillis
())
.
setOngoing
(
true
);
}
//Couldn't expand RemoteViews
try
{
updateNotivView
();
builder
.
setCustomContentView
(
NormalView
);
NormalView
.
setOnClickPendingIntent
(
R
.
id
.
id_notification_click
,
getFruitIntent
);
notification
=
builder
.
build
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
// startForeground(ID_FOR_CUSTOM_VIEW, notification);
// Boolean isOpen = (Boolean) SPUtils.get(context, SPUtils.SP_LONG_NOTICATION, true);
Boolean
isOpen
=
true
;
try
{
if
(
isOpen
)
{
if
(
notification
!=
null
)
{
startForeground
(
ID_FOR_CUSTOM_VIEW
,
notification
);
}
}
}
catch
(
Exception
e
)
{
LogUtils
.
e
(
TAG
,
"set service for push exception: "
,
e
);
}
}
private
void
updateNotivView
()
{
//Couldn't expand RemoteViews
try
{
if
(
NormalView
==
null
)
{
NormalView
=
new
RemoteViews
(
context
.
getPackageName
(),
R
.
layout
.
notification_view
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
class
ActionReceiver
extends
BroadcastReceiver
{
@Override
public
void
onReceive
(
Context
context
,
Intent
data
)
{
if
(
data
==
null
||
data
.
getAction
()
==
null
)
{
return
;
}
// collapseStatusBar(context);
collapseStatusBar
();
switch
(
data
.
getAction
())
{
case
NOTIFICATION_GET_FRUIT:
Intent
intent2
=
new
Intent
(
context
,
GameActivity
.
class
);
intent2
.
setFlags
(
Intent
.
FLAG_ACTIVITY_NEW_TASK
);
startActivity
(
intent2
);
break
;
default
:
break
;
}
}
}
//9.0通知栏 适配
public
static
String
createNotificationChannel
(
Context
context
)
{
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
O
)
{
String
channelId
=
"luck_farm"
;
CharSequence
channelName
=
"幸运农场"
;
String
channelDescription
=
"快速收获"
;
int
channelImportance
=
NotificationManager
.
IMPORTANCE_MIN
;
NotificationChannel
notificationChannel
=
new
NotificationChannel
(
channelId
,
channelName
,
channelImportance
);
// 设置描述 最长30字符
notificationChannel
.
setDescription
(
channelDescription
);
// 该渠道的通知是否使用震动
notificationChannel
.
enableVibration
(
false
);
// 设置显示模式
notificationChannel
.
setLockscreenVisibility
(
NotificationCompat
.
VISIBILITY_PUBLIC
);
NotificationManager
notificationManager
=
(
NotificationManager
)
context
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
);
notificationManager
.
createNotificationChannel
(
notificationChannel
);
return
channelId
;
}
else
{
return
null
;
}
}
public
void
collapseStatusBar
(
Context
context
)
{
try
{
@SuppressLint
(
"WrongConstant"
)
Object
statusBarManager
=
context
.
getSystemService
(
"statusbar"
);
Method
collapse
;
if
(
Build
.
VERSION
.
SDK_INT
<=
16
)
{
collapse
=
statusBarManager
.
getClass
().
getMethod
(
"collapse"
);
}
else
{
collapse
=
statusBarManager
.
getClass
().
getMethod
(
"collapsePanels"
);
}
collapse
.
invoke
(
statusBarManager
);
}
catch
(
Exception
localException
)
{
localException
.
printStackTrace
();
}
}
public
void
collapseStatusBar
()
{
@SuppressLint
(
"WrongConstant"
)
Object
service
=
getSystemService
(
"statusbar"
);
if
(
null
==
service
)
return
;
try
{
Class
<?>
clazz
=
Class
.
forName
(
"android.app.StatusBarManager"
);
int
sdkVersion
=
android
.
os
.
Build
.
VERSION
.
SDK_INT
;
Method
collapse
=
null
;
if
(
sdkVersion
<=
16
)
{
collapse
=
clazz
.
getMethod
(
"collapse"
);
}
else
{
collapse
=
clazz
.
getMethod
(
"collapsePanels"
);
}
collapse
.
setAccessible
(
true
);
collapse
.
invoke
(
service
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
app/src/main/res/layout/notification_view.xml
0 → 100644
View file @
464b75dd
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
android:id=
"@+id/id_notification_click"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:background=
"@mipmap/notif_bg"
android:paddingLeft=
"@dimen/dp_14"
android:paddingRight=
"@dimen/dp_14"
>
<ImageView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_centerVertical=
"true"
android:src=
"@mipmap/notif_icon"
/>
<ImageView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_alignParentRight=
"true"
android:layout_centerVertical=
"true"
android:src=
"@mipmap/notif_btn"
/>
</RelativeLayout>
\ No newline at end of file
app/src/main/res/mipmap-xhdpi/notif_bg.png
0 → 100644
View file @
464b75dd
2.66 KB
app/src/main/res/mipmap-xhdpi/notif_btn.png
0 → 100644
View file @
464b75dd
11.5 KB
app/src/main/res/mipmap-xhdpi/notif_icon.png
0 → 100644
View file @
464b75dd
19 KB
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