今天照著《Android第一行程式碼第二版-郭霖》把通知那部分的程式碼實現了一下,結果發現 Notification notification = new NotificationCompat.Builder(this)過時了。
我查了一下開發者檔案,根據檔案可以看到public Builder (Context context)已經過時了,需要在原來的基礎上加上一個引數String型別的channelId。對於這個channelId,檔案裡並沒有說明這個的具體含義,只是說通知將釋出在此NotificationChannel???
不管了先隨便設一個值看看效果,程式碼如下,佈局檔案的程式碼就不貼出來了,就一個按鈕。
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
NotificationManager manager ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendNotice = findViewById(R.id.send_notice);
sendNotice.setOnClickListener(this);
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.send_notice:
Notification notification = new NotificationCompat.Builder(this,"myChannelId")
.setContentTitle("This is content title")
.setContentText("This is content text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.build();
manager.notify(1,notification);
break;
default:
break;
}
}
}
執行結果如下:
根據檢視別的部落格才知道要為通知註冊渠道,要不然不會發出通知,程式碼修改如下:
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
NotificationManager manager ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendNotice = findViewById(R.id.send_notice);
sendNotice.setOnClickListener(this);
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.send_notice:
//高版本需要渠道
if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
//只在Android O之上需要渠道
NotificationChannel notificationChannel = new NotificationChannel("myChannelId","channelname"
,NotificationManager.IMPORTANCE_HIGH);
//如果這裡用IMPORTANCE_NOENE就需要在系統的設定裡面開啟渠道,通知才能正常彈出
manager.createNotificationChannel(notificationChannel);
}
Notification notification = new NotificationCompat.Builder(this,"myChannelId")
.setContentTitle("This is content title")
.setContentText("This is content text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.build();
manager.notify(1,notification);
break;
default:
break;
}
}
}
關鍵程式碼就在下面這段程式碼,其目的就是為了註冊渠道,之後就可以使用這個channelId來發通知了。
if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
//只在Android O之上需要渠道
NotificationChannel notificationChannel = new NotificationChannel("myChannelId","channelname"
,NotificationManager.IMPORTANCE_HIGH);
//如果這裡用IMPORTANCE_NOENE就需要在系統的設定裡面開啟渠道,通知才能正常彈出
manager.createNotificationChannel(notificationChannel);
}
結果如下: