Android8(包含8)以上需要创建channel id,否则无法弹出notification:
public class NotificationTest extends AppCompatActivity {
String channel_id="myChannelId"; //每个notification对应的channel_id是唯一的,一般用包名.channel命名
String channel_name="myChannelName";
String description = "this is myChannel's description";
NotificationManager notificationManager;
Button sendNotification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_test);
sendNotification = findViewById(R.id.sendNotification);
createNotificationChannel(); //为notification创建channel_id
sendNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(NotificationTest.this, NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(NotificationTest.this, 0, intent, 0);
Notification notification = new NotificationCompat.Builder(NotificationTest.this, channel_id)
.setContentTitle("Notification Title")
.setContentText("Notification Info")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), com.tripod.permission.R.drawable.ic_launcher_background))
.setContentIntent(pi) //设置pendingIntent ,是的Notification为可点击状态
.setAutoCancel(true) //设置notification点击查看后,后台显示的小图标自动取消
.build();//创建notification
notificationManager.notify(1,notification); //显示notification;每个notification对应的id都是唯一的
}
});
}
/*Android 8.0(included)以上弹出Notification需要为其创建channel_id,否则存在Notification无法在后台显示的弹出*/
private void createNotificationChannel(){
//Android8.0(API26)以上需要调用下列方法,但低版本由于支持库旧,不支持调用,需做判断
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(channel_id,channel_name,importance);
channel.setDescription(description);
notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);//获取notification服务
notificationManager.createNotificationChannel(channel);//创建channel_id
}
}
}
//NotificationTest.java 对应布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NotificationTest">
<Button
android:id="@+id/sendNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sendNotification " />
</LinearLayout>
public class NotificationActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
}
}
// NotificationActivity 对应布局 (下拉菜单,点击notification弹窗后,弹出的界面)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NotificationActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is notofcation layout" />
</LinearLayout>
标签:layout,Notification,使用,关于,notification,android,id,channel From: https://www.cnblogs.com/a-n-yan/p/17248587.html