1.创建通知,并显示
/**
* 简易通知显示
* 必要三元素:smallIcon,contentTitle,contentText
*/
public static void showNotice(Context context, CharSequence msg){
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("渠道ID", "优惠券商品", NotificationManager.IMPORTANCE_LOW);
manager.createNotificationChannel(channel);
builder = new NotificationCompat.Builder(context, "渠道ID");
}else {
builder = new NotificationCompat.Builder(context);
}
Notification notification = builder
.setSmallIcon(android.R.drawable.sym_def_app_icon)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(msg)
.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
manager.notify(0,notification);
}
- 注意:Android8.0及以上系统版本
a.一定要设置通知渠道
b.且new NotificationChannel("渠道ID"
这里的渠道ID和下面的new NotificationCompat.Builder(this, "渠道ID")
中的渠道ID要相同
2.点击通知,跳转到主页面
private void showNotice(Context context,String msg){
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("渠道ID", "优惠券商品", NotificationManager.IMPORTANCE_LOW);
manager.createNotificationChannel(channel);
builder = new NotificationCompat.Builder(context, "渠道ID");
}else {
builder = new NotificationCompat.Builder(context);
}
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = builder
.setSmallIcon(R.mipmap.logo)
.setContentTitle("我是一个标题")
.setContentText(msg)
.setContentIntent(contentIntent)
.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
manager.notify(0,notification);
}
3.点击通知,打开多个页面
private void showNotice(Context context,String msg){
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("渠道ID", "优惠券商品", NotificationManager.IMPORTANCE_LOW);
manager.createNotificationChannel(channel);
builder = new NotificationCompat.Builder(context, "渠道ID");
}else {
builder = new NotificationCompat.Builder(context);
}
PendingIntent contentIntent = PendingIntent.getActivities(getApplicationContext(), 0, makeIntentStack(context), PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = builder
.setSmallIcon(R.mipmap.logo)
.setContentTitle("我是一个标题")
.setContentText(msg)
.setContentIntent(contentIntent)
.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
manager.notify(0,notification);
}
Intent[] makeIntentStack(Context context) {
Intent[] intents = new Intent[2];
intents[0] = Intent.makeRestartActivityTask(new ComponentName(context, MainActivity.class));
intents[1] = new Intent(context, WhiteBarActivity.class);
return intents;
}
- 这三种方式,第二种应该是使用场景最多的。
参考:Notification点击跳转指定的Activity
其实也可以用AndroidStudio创建一个Notification,只需修改部分参数即可