-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
wufanghan
committed
Mar 26, 2021
1 parent
fb2456b
commit 8580571
Showing
5 changed files
with
348 additions
and
1 deletion.
There are no files selected for viewing
4 changes: 3 additions & 1 deletion
4
app/src/main/java/com/example/baseframe/data/bean/ArticlesBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
app/src/main/java/com/example/baseframe/utils/push/NotificationCompatUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package com.example.baseframe.utils.push | ||
|
||
import android.app.Notification | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.app.PendingIntent | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Build | ||
import android.provider.Settings | ||
import android.text.TextUtils | ||
import androidx.annotation.RequiresApi | ||
import androidx.core.app.NotificationCompat | ||
|
||
|
||
/** | ||
* 通知兼容工具类 | ||
* | ||
* 本类中的代码使用Android支持库中的NotificationCompatAPI。 | ||
* 这些API允许您添加仅在较新版本Android上可用的功能,同时仍向后兼容Android4.0(API级别14)。 | ||
* 但是,诸如内嵌回复操作等部分新功能在较旧版本上会导致发生空操作。 | ||
*/ | ||
class NotificationCompatUtil { | ||
companion object { | ||
|
||
/** | ||
* 创建通知 | ||
* @param context 上下文 | ||
* @param channel 通知渠道 | ||
* @param title 标题 | ||
* @param text 正文文本 | ||
* @param intent 对点按操作做出响应意图 | ||
* @return | ||
*/ | ||
fun createNotificationBuilder( | ||
context: Context, | ||
channel: Channel, | ||
title: CharSequence? = null, | ||
text: CharSequence? = null, | ||
intent: Intent? = null | ||
): NotificationCompat.Builder { | ||
// 必须先创建通知渠道,然后才能在Android 8.0及更高版本上发布任何通知 | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
createChannel(context, channel) | ||
} | ||
|
||
val builder = | ||
NotificationCompat.Builder(context, channel.channelId) | ||
.setPriority(getLowVersionPriority(channel)) // 通知优先级,优先级确定通知在Android7.1和更低版本上的干扰程度。 | ||
.setVisibility(channel.lockScreenVisibility) // 锁定屏幕公开范围 | ||
.setVibrate(channel.vibrate) // 震动模式 | ||
.setSound(channel.sound ?: Settings.System.DEFAULT_NOTIFICATION_URI) // 声音 | ||
.setOnlyAlertOnce(true) // 设置通知只会在通知首次出现时打断用户(通过声音、振动或视觉提示),而之后更新则不会再打断用户。 | ||
|
||
// 标题,此为可选内容 | ||
if (!TextUtils.isEmpty(title)) builder.setContentTitle(title) | ||
|
||
// 正文文本,此为可选内容 | ||
if (!TextUtils.isEmpty(text)) builder.setContentText(text) | ||
|
||
// 设置通知的点按操作,每个通知都应该对点按操作做出响应,通常是在应用中打开对应于该通知的Activity。 | ||
if (intent != null) { | ||
val pendingIntent = | ||
PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) | ||
builder.setContentIntent(pendingIntent) | ||
.setAutoCancel(true) // 在用户点按通知后自动移除通知 | ||
if(NotificationManager.IMPORTANCE_HIGH == channel.importance) builder.setFullScreenIntent(pendingIntent, false) | ||
} | ||
|
||
return builder | ||
} | ||
|
||
/** | ||
* 获取低版本的优先级 | ||
* 要支持搭载 Android 7.1(API 级别 25)或更低版本的设备, | ||
* 您还必须使用 NotificationCompat 类中的优先级常量针对每条通知调用 setPriority()。 | ||
* @param channel | ||
* @return | ||
*/ | ||
private fun getLowVersionPriority(channel: Channel): Int { | ||
return when (channel.importance) { | ||
NotificationManager.IMPORTANCE_HIGH -> NotificationCompat.PRIORITY_HIGH | ||
NotificationManager.IMPORTANCE_LOW -> NotificationCompat.PRIORITY_LOW | ||
NotificationManager.IMPORTANCE_MIN -> NotificationCompat.PRIORITY_MIN | ||
else -> NotificationCompat.PRIORITY_DEFAULT | ||
} | ||
} | ||
|
||
/** | ||
* 创建通知渠道 | ||
* <p> | ||
* 反复调用这段代码也是安全的,因为创建现有通知渠道不会执行任何操作。 | ||
* 注意:创建通知渠道后,您便无法更改通知行为,此时用户拥有完全控制权。不过,您仍然可以更改渠道的名称和说明。 | ||
* @param context 上下文 | ||
* @param channel 通知渠道 | ||
*/ | ||
@RequiresApi(api = Build.VERSION_CODES.O) | ||
private fun createChannel( | ||
context: Context, | ||
channel: Channel | ||
) { | ||
val notificationChannel = | ||
NotificationChannel(channel.channelId, channel.name, channel.importance) | ||
notificationChannel.description = channel.description // 描述 | ||
notificationChannel.vibrationPattern = channel.vibrate // 震动模式 | ||
notificationChannel.setSound(channel.sound ?: Settings.System.DEFAULT_NOTIFICATION_URI, notificationChannel.audioAttributes) // 声音 | ||
val notificationManager = | ||
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
notificationManager.createNotificationChannel(notificationChannel) | ||
} | ||
|
||
/** | ||
* 显示通知 | ||
* | ||
* | ||
* 请记得保存您传递到 NotificationManagerCompat.notify() 的通知 ID,因为如果之后您想要更新或移除通知,将需要使用这个 ID。 | ||
* @param context 上下文 | ||
* @param id 通知的唯一ID | ||
* @param notification 通知 | ||
*/ | ||
fun notify( | ||
context: Context, | ||
id: Int, | ||
notification: Notification? | ||
) { | ||
val notificationManager = | ||
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
notificationManager.notify(id, notification) | ||
} | ||
|
||
/** | ||
* 取消通知 | ||
* @param context 上下文 | ||
* @param id 通知的唯一ID | ||
*/ | ||
fun cancel(context: Context, id: Int) { | ||
val notificationManager = | ||
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
notificationManager.cancel(id) | ||
} | ||
|
||
/** | ||
* 取消所有通知 | ||
* @param context 上下文 | ||
*/ | ||
fun cancelAll(context: Context) { | ||
val notificationManager = | ||
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
notificationManager.cancelAll() | ||
} | ||
} | ||
|
||
/** | ||
* 通知渠道 | ||
*/ | ||
data class Channel( | ||
val channelId: String, // 唯一渠道ID | ||
val name: CharSequence, // 用户可见名称 | ||
val importance: Int, // 重要性级别 | ||
val description: String? = null, // 描述 | ||
@NotificationCompat.NotificationVisibility | ||
val lockScreenVisibility: Int = NotificationCompat.VISIBILITY_SECRET, // 锁定屏幕公开范围 | ||
val vibrate: LongArray? = null, // 震动模式 | ||
val sound: Uri? = null // 声音 | ||
) | ||
} |
174 changes: 174 additions & 0 deletions
174
app/src/main/java/com/example/baseframe/utils/push/PushNotificationHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
package com.example.baseframe.utils.push | ||
|
||
import android.app.Notification | ||
import android.app.NotificationManager | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.Uri | ||
import androidx.core.app.NotificationCompat | ||
import com.example.baseframe.App | ||
import com.example.baseframe.R | ||
import com.example.baseframe.ui.main.MainNewActivity | ||
import com.example.baseframe.utils.Utils.getContext | ||
|
||
|
||
object PushNotificationHelper { | ||
|
||
/** 通知渠道-聊天消息(重要性级别-高:发出声音) */ | ||
private val MESSAGE = NotificationCompatUtil.Channel( | ||
channelId = "MESSAGE", | ||
name = App.getInstance().getString(R.string.channel_message), | ||
importance = NotificationManager.IMPORTANCE_DEFAULT | ||
) | ||
|
||
/** 通知渠道-@提醒消息(重要性级别-紧急:发出提示音,并以浮动通知的形式显示 & 锁屏显示 & 振动0.25s )*/ | ||
private val MENTION = NotificationCompatUtil.Channel( | ||
channelId = "MENTION", | ||
name = App.getInstance().getString(R.string.channel_mention), | ||
importance = NotificationManager.IMPORTANCE_HIGH, | ||
lockScreenVisibility = NotificationCompat.VISIBILITY_PUBLIC, | ||
vibrate = longArrayOf(0, 250) | ||
) | ||
|
||
/** 通知渠道-系统通知(重要性级别-中:无提示音) */ | ||
private val NOTICE = NotificationCompatUtil.Channel( | ||
channelId = "NOTICE", | ||
name = App.getInstance().getString(R.string.channel_notice), | ||
importance = NotificationManager.IMPORTANCE_LOW | ||
) | ||
|
||
/** 通知渠道-音视频通话(重要性级别-紧急:发出提示音,并以浮动通知的形式显示 & 锁屏显示 & 振动4s停2s再振动4s ) */ | ||
private val CALL = NotificationCompatUtil.Channel( | ||
channelId = "CALL", | ||
name = App.getInstance().getString(R.string.channel_call), | ||
importance = NotificationManager.IMPORTANCE_HIGH, | ||
lockScreenVisibility = NotificationCompat.VISIBILITY_PUBLIC, | ||
vibrate = longArrayOf(0, 4000, 2000, 4000), | ||
sound = Uri.parse("android.resource://" + App.getInstance().packageName + "/" + R.raw.iphone) | ||
) | ||
|
||
/** | ||
* 显示聊天消息 | ||
* @param context 上下文 | ||
* @param id 通知的唯一ID | ||
* @param title 标题 | ||
* @param text 正文文本 | ||
*/ | ||
fun notifyMessage( | ||
context: Context, | ||
id: Int, | ||
title: String?, | ||
text: String? | ||
) { | ||
val intent = Intent(context, MainNewActivity::class.java) | ||
|
||
val builder = NotificationCompatUtil.createNotificationBuilder( | ||
context, | ||
MESSAGE, | ||
title, | ||
text, | ||
intent | ||
) | ||
|
||
// 默认情况下,通知的文字内容会被截断以放在一行。如果您想要更长的通知,可以使用 setStyle() 添加样式模板来启用可展开的通知。 | ||
builder.setStyle( | ||
NotificationCompat.BigTextStyle() | ||
.bigText(text) | ||
) | ||
|
||
NotificationCompatUtil.notify(context, id, buildDefaultConfig(builder)); | ||
} | ||
|
||
/** | ||
* 显示@提醒消息 | ||
* @param context 上下文 | ||
* @param id 通知的唯一ID | ||
* @param title 标题 | ||
* @param text 正文文本 | ||
*/ | ||
fun notifyMention( | ||
context: Context, | ||
id: Int, | ||
title: String?, | ||
text: String? | ||
) { | ||
val intent = Intent(context, MainNewActivity::class.java) | ||
|
||
val builder = NotificationCompatUtil.createNotificationBuilder( | ||
context, | ||
MENTION, | ||
title, | ||
text, | ||
intent | ||
) | ||
|
||
// 默认情况下,通知的文字内容会被截断以放在一行。如果您想要更长的通知,可以使用 setStyle() 添加样式模板来启用可展开的通知。 | ||
builder.setStyle( | ||
NotificationCompat.BigTextStyle() | ||
.bigText(text) | ||
) | ||
|
||
NotificationCompatUtil.notify(context, id, buildDefaultConfig(builder)); | ||
} | ||
|
||
/** | ||
* 显示系统通知 | ||
* @param context 上下文 | ||
* @param id 通知的唯一ID | ||
* @param title 标题 | ||
* @param text 正文文本 | ||
*/ | ||
fun notifyNotice( | ||
context: Context, | ||
id: Int, | ||
title: String?, | ||
text: String? | ||
) { | ||
val intent = Intent(context, MainNewActivity::class.java) | ||
|
||
val builder = NotificationCompatUtil.createNotificationBuilder( | ||
context, | ||
NOTICE, | ||
title, | ||
text, | ||
intent | ||
) | ||
|
||
NotificationCompatUtil.notify(context, id, buildDefaultConfig(builder)); | ||
} | ||
|
||
/** | ||
* 显示音视频通话 | ||
* @param context 上下文 | ||
* @param id 通知的唯一ID | ||
* @param title 标题 | ||
* @param text 正文文本 | ||
*/ | ||
fun notifyCall( | ||
context: Context, | ||
id: Int, | ||
title: String?, | ||
text: String? | ||
) { | ||
val intent = Intent(context, MainNewActivity::class.java) | ||
|
||
val builder = NotificationCompatUtil.createNotificationBuilder( | ||
context, | ||
CALL, | ||
title, | ||
text, | ||
intent | ||
) | ||
|
||
NotificationCompatUtil.notify(context, id, buildDefaultConfig(builder)); | ||
} | ||
|
||
/** | ||
* 构建应用通知的默认配置 | ||
* @param builder 构建器 | ||
*/ | ||
private fun buildDefaultConfig(builder: NotificationCompat.Builder): Notification { | ||
builder.setSmallIcon(R.drawable.ic_launcher_foreground) | ||
return builder.build() | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters