之前有总结过一些问题,跳转,还有声音等问题。
MonkeyLei:Android 8.0后notification通知声音无法关闭+更新应用通知进度时总是不停的响
MonkeyLei:通知栏推送点击跳转以及返回主界面-第二弹-完善纠正待续
最近学习Hook入门知识,准备实践下Hook通知这块。所以温习一下创建基本的通知的方式。然后想到尝试官方教程,发现其实教程创建步骤写的很清楚的,你要相信,搜到的文章,几乎都是基于官方的教程来的,只不过人家总结的更易懂,同时采坑的问题也都避免了很多。
So,我们还是基于官方看看吧。如何解决问题,习惯官方文档,习惯去习惯。。
1. 首先就去搜下NotificationManager
NotificationChannel上面,所以需要单独设置该属性。
开篇就有提到创建导航栏
2. 我们点击过去看看Notifications Overview | Android Developers
解释:人家都给的详细了,文本,标题,小图标,大图标,应用名称,显示时间等。以及后面介绍的reply,自定义布局,震动,优先级等。
3. 接下来,有Create a Notification
建造者模式鸭...
4. 到此我们开发的思路,流程,教程已经备齐,假设你有音乐通知栏开发的需求,可以细细瞅瞅。。 Create a Notification | Android Developers
你还要是喜欢日语、中文,你可以 。有些页面内容可能还是英文。
直接贴出案例 - 有注释链接 - 问题不大!
/**
* 发送通知(支持8.0+)
* @param view
*/
public void nofify(View view) {
// 1. Set the notification content - 创建通知基本内容
// https://developer.android.google.cn/training/notify-user/build-notification.html#builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("My notification")
// 这是单行
//.setContentText("Much longer text that cannot fit one line...")
// 这是多行
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Much longer text that cannot fit one line..." +
"Much longer text that cannot fit one line..." +
"Much longer text that cannot fit one line..."))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true);
// 2. Create a channel and set the importance - 8.0后需要设置Channel
// https://developer.android.google.cn/training/notify-user/build-notification.html#builder
createNotificationChannel();
// 3. Set the notification's tap action - 创建一些点击事件,比如点击跳转页面
// https://developer.android.google.cn/training/notify-user/build-notification.html#click
// 4. Show the notification - 展示通知
// https://developer.android.google.cn/training/notify-user/build-notification.html#notify
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
notificationManager.notify((int) System.currentTimeMillis(), builder.build());
}
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.app_name);
String description = getString(R.string.app_name);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
自定义通知栏样式,封装成模块,然后任何地方都可以直接调用,也可以统一管理样式。我之前那样做的,没啥问题!
OK。。继续Hook通知实践。。。。加油!