如果你将项目中的targetSdkVersion指定到了26或者更高(targetSdkVersion可以在app/build.gradle里查看),这个时候如果还不使用通知渠道的话,那么你的App的通知将完全无法弹出。
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.tencent.testand.R;
public class NotificationActivity extends Activity implements View.OnClickListener {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
findViews();
// API Level >= 26,需要添加NotificationChannel
// Build.VERSION.SDK_INT 获取当前SDK的API Level
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "MyCustomChannel";
String channelName = "自定义Channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
createNotificationChannel(channelId, channelName, importance);
channelId = "subscribe";
channelName = "订阅消息";
importance = NotificationManager.IMPORTANCE_DEFAULT;
createNotificationChannel(channelId, channelName, importance);
}
}
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel(String channelId, String channelName, int importance) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
// 是否显示角标
channel.setShowBadge(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.chat_button: {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = manager.getNotificationChannel("MyCustomChannel");
if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getId());
startActivity(intent);
Toast.makeText(this, "请手动将通知打开", Toast.LENGTH_SHORT).show();
}
}
Notification notification = new NotificationCompat.Builder(this, "MyCustomChannel")
.setContentTitle("收到一条聊天消息")
.setContentText("这是我自定义的聊天内容")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.icon)
// 这里需要用drawable例的图片
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
// 设置显示角标的数量(注:如果需要显示出来,需要长按图标)
.setNumber(2)
.setAutoCancel(true).build();
manager.notify(1, notification);
break;
}
case R.id.subscribe_button: {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this, "subscribe")
.setContentTitle("收到一条订阅消息")
.setContentText("北京市教委:明天要放假了~")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setAutoCancel(true).build();
manager.notify(2, notification);
break;
}
}
}
private void findViews() {
Button chatButton = findViewById(R.id.chat_button);
chatButton.setOnClickListener(this);
Button subscribeButton = findViewById(R.id.subscribe_button);
subscribeButton.setOnClickListener(this);
}
}
Android 8.0还赋予了我们删除通知渠道的功能,只需使用如下代码即可删除:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.deleteNotificationChannel(channelId);
网友评论