Android studio 28 通知栏报错Failed to post notification on channel "null"
今天老师上课上到通知,教材是第一行代码,可能教材上的源码太久了,我在用安卓9的时候,模拟器报错Failed to post notification on channel “null”,在网上查了很久才知道,Notification在android 26以上设置时,需要设置渠道信息才可以正常显示通知,这是
官方文档地址[https://developer.android.google.cn/about/versions/pie/android-9.0]
接下来说说我的解决办法,首先建立布局,布局文件非常简单,里面只有一个发送通知按钮,用于发出一条通知
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/send_notice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send notifi"
/>
</LinearLayout>
然后修改MainActivity代码,懒得找图片了,直接设置了ic_launcher这张图
package com.example.ch08;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendNotice = findViewById(R.id.send_notice);
sendNotice.setOnClickListener(this);
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.send_notice:
Intent intent = new Intent(this,NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
String id = "channel_001";
String name = "name";
NotificationManager notificationManager = (NotificationManager)
this.getSystemService(NOTIFICATION_SERVICE);
Notification notification = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//判断API
NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(mChannel);
notification = new Notification.Builder(this)
.setChannelId(id)
.setContentTitle("通知")
.setContentText("你有一个新的通知")
.setContentIntent(pi)
.setLights(Color.GREEN, 1000, 1000)//设置三色灯
.setSmallIcon(R.mipmap.ic_launcher_round).build();
}else{
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("通知")
.setContentText("你有新的通知")
.setSmallIcon(R.mipmap.ic_launcher_round)
.setOngoing(true)
.setContentIntent(pi)
.setLights(Color.GREEN, 1000, 1000)//设置三色灯
.setChannelId(id);//无效
notification = notificationBuilder.build();
}
notificationManager.notify(1,notification); //5
break;
default:
break;
}
}
}
最后运行一下,这是效果图
我这里做了两个页面,下拉通知栏可以看见通知,点击通知后会进入第二个界面
Intent intent = new Intent(this,NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
.setContentIntent(pi)
加入这几行代码就可以了
还是要多关心一下技术圈的更新,就不用找的这么辛苦了