这里是转载了两家的文章,只是i为了方便后面使用。
原文链接:
Android 消息通知栏Notification使用和权限
https://blog.youkuaiyun.com/kangweijian/article/details/89646201?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_utm_term-4&spm=1001.2101.3001.4242Android10.0通知Notification的使用
https://blog.youkuaiyun.com/shanshui911587154/article/details/105683683/
1.查看Notification权限
public class NotificationUtils {
public static boolean checkNotifySetting(Context context) {
NotificationManagerCompat manager = NotificationManagerCompat.from(context);
// areNotificationsEnabled方法的有效性官方只最低支持到API 19,低于19的仍可调用此方法不过只会返回true,即默认为用户已经开启了通知。
return manager.areNotificationsEnabled();
}
}
2.打开通知栏设置界面
//通知栏权限
private void checkNotify(){
if(!NotificationUtils.checkNotifySetting(MainActivity.this)){
MyAlertDialog myAlertDialog = new MyAlertDialog(this).builder()
.setTitle("通知权限")
.setMsg("尚未开启通知权限,点击去开启")
.setPositiveButton("确认", new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
//这种方案适用于 API 26, 即8.0(含8.0)以上可以用
intent.putExtra(EXTRA_APP_PACKAGE, getPackageName());
intent.putExtra(EXTRA_CHANNEL_ID, getApplicationInfo().uid);
//这种方案适用于 API21——25,即 5.0——7.1 之间的版本可以使用
intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", getApplicationInfo().uid);
// 小米6 -MIUI9.6-8.0.0系统,是个特例,通知设置界面只能控制"允许使用通知圆点"——然而这个玩意并没有卵用,我想对雷布斯说:I'm not ok!!!
// if ("MI 6".equals(Build.MODEL)) {
// intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
// Uri uri = Uri.fromParts("package", getPackageName(), null);
// intent.setData(uri);
// // intent.setAction("com.android.settings/.SubSettings");
// }
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
// 出现异常则跳转到应用设置界面:锤子坚果3——OC105 API25
Intent intent = new Intent();
//下面这种方案是直接跳转到当前应用的设置界面。
//https://blog.youkuaiyun.com/ysy950803/article/details/71910806
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
}
}).setNegativeButton("取消", new View.OnClickListener() {
@Override
public void onClick(View v) {}
});
myAlertDialog.show();
}
}
Notification通知栏使用
https://github.com/yangchong211/YCNotification
3.创建渠道
由于我们只需要在android8.0以上创建,所以在代码中进行判断:
private String createNotificationChannel(String channelID, String channelNAME, int level) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
manager.createNotificationChannel(channel);
return channelID;
} else {
return null;
}
}
4.创建通知栏消息
Intent intent = new Intent(this, Main2Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
String channelId = createNotificationChannel("my_channel_ID", "my_channel_NAME", NotificationManager.IMPORTANCE_HIGH);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, channelId)
.setContentTitle("通知")
.setContentText("收到一条消息")
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(100, notification.build());
创建一个通知栏消息就是以上四步。完毕。其他详细方法可以自己去看原文。