專案網址https://github.com/uuko/android_project/tree/master/Notifaction
參考文章 android官方document+codelab
每個通知通道代表一種通知類型。
在您的代碼中,您可以在每個通知通道中將多個通知分組。
對於每個通知通道,您的應用程序設置行為的通道,並且行為被應用到該頻道的所有通知。例如,您的應用可能會在頻道中設置通知以播放聲音,閃爍燈光或振動。
無論您的應用為通知渠道設置了什麼行為,用戶都可以更改該行為,並且用戶可以完全關閉您的應用的通知。
//每個channel與唯一id相連
private static final String PRIMARY_CHANNEL_ID ="primary_notification_channel";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button notify=(Button) findViewById(R.id.notify);
notify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendNotification();
}
});
** createNotificationChannel();**
}
public void sendNotification(){
//builder實例
NotificationCompat.Builder notifyBuilder = getNotificationBuilder();
notificationManager.**notify**(NOTIFICATION_ID, notifyBuilder.build());
}
//notificationmanger >傳遞給用戶的
private NotificationManager notificationManager;
public void createNotificationChannel()
{//實體化manager並設置一些參數
notificationManager=
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.O){
/*創建一個channel對象 public NotificationChannel (String id,
CharSequence name,
int importance)*/
NotificationChannel notificationChannel=
new NotificationChannel(PRIMARY_CHANNEL_ID,
"123" ,NotificationManager.IMPORTANCE_HIGH
);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setDescription("hihi");
//正式創建 notificationManager.createNotificationChannel(notificationChannel);
}
}
//使用NotificationCompat.Builder該類創建通知
//需要一個ID讓通知相關 以好取消OR更新
private static final int NOTIFICATION_ID = 0;
private NotificationCompat.Builder getNotificationBuilder(){
Intent notifyIntent=new Intent(this,MainActivity.class);
//intent必須包在pendingintent裡面
PendingIntent notifyPendingIntent=PendingIntent.getActivity(this,
NOTIFICATION_ID,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//FLAG_UPDATE_CURRENT>第二條會替換第一條內容 CANCEL是會變點第一條沒反應只有第二條可以點
NotificationCompat.Builder notifyBuilder=
new NotificationCompat.Builder(this,PRIMARY_CHANNEL_ID)
.setContentTitle("notify~~~")
.setContentText("uuko notify!!")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentIntent(notifyPendingIntent)//加入intent
.setAutoCancel(true);
return notifyBuilder;
}
觀念>
通知為您的應用提供了一種與用戶進行交互的方式,即使該應用未運行也是如此。
當Android發布通知時,該通知首先顯示為設備通知區域中的圖標。
(這可以用intent+pendingintent去做 再加broadcast)
要指定通知的UI和操作,請使用NotificationCompat.Builder。
要創建通知,請使用NotificationCompat.Builder.build()。
要發出通知,請使用NotificationManager.notify()將該通知對像傳遞給Android運行時系統。
為了使更新或取消通知成為可能,請將通知ID與通知相關聯。
通知可以包含多個組件,包括一個小圖標(setSmallIcon(),必填);標題(setContentTitle()); 以及詳細的文字(setContentText())。
通知還可以包括待處理的意圖,擴展的樣式,優先級等。有關更多詳細信息,請參見 NotificationCompat.Builder。
//功能拓展
public class MainActivity extends AppCompatActivity {
private static final String PRIMARY_CHANNEL_ID = "primary_notification_channel";
private NotificationManager notificationManager;
private static final int NOTIFICATION_ID = 0;
private static final String ACTION_UPDATE_NOTIFICATION =
"com.example.android.Notifaction.ACTION_UPDATE_NOTIFICATION";
private NotificationReciever notificationReciever;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationReciever=new NotificationReciever();
Button update=(Button) findViewById(R.id.update);
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateNotification();
}
});
Button cancel=(Button) findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cancelupdateNotification();
}
});
Button notify=(Button) findViewById(R.id.notify);
notify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendNotification();
}
});
createNotificationChannel();
setbuttonstate(true,false,false);
registerReceiver(notificationReciever,new IntentFilter(ACTION_UPDATE_NOTIFICATION));
}
@Override
public void onDestroy(){
unregisterReceiver(notificationReciever);
super.onDestroy();
}
//更新
public void updateNotification(){
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.mascot_1);
NotificationCompat.Builder notifybuilder=getNotificationBuilder();
notifybuilder.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap)
.setBigContentTitle("bigggg"));
notificationManager.notify(NOTIFICATION_ID,notifybuilder.build());
setbuttonstate(false,true,true);
}
//取消通知
public void cancelupdateNotification(){
notificationManager.cancel(NOTIFICATION_ID);
}
public void sendNotification(){
Intent intent=new Intent(ACTION_UPDATE_NOTIFICATION);
PendingIntent pendingIntent=PendingIntent.getBroadcast(this,NOTIFICATION_ID,intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notifyBuilder = getNotificationBuilder();
notifyBuilder.addAction(R.drawable.ic_update,"update",pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notifyBuilder.build());
setbuttonstate(false,true,true);
}
void setbuttonstate(Boolean isNotifyEnabled,Boolean isUpdadateEnabled,Boolean isCancelEnabled){
Button update=(Button) findViewById(R.id.update);
Button notify=(Button) findViewById(R.id.notify);
Button cancel=(Button) findViewById(R.id.cancel);
notify.setEnabled(isNotifyEnabled);
update.setEnabled(isUpdadateEnabled);
cancel.setEnabled(isCancelEnabled);
}
public void createNotificationChannel(){
notificationManager=
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.O){
NotificationChannel notificationChannel=
new NotificationChannel(PRIMARY_CHANNEL_ID,
"123" ,NotificationManager.IMPORTANCE_HIGH
);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setDescription("hihi");
notificationManager.createNotificationChannel(notificationChannel);
}
}
private NotificationCompat.Builder getNotificationBuilder(){
Intent notifyIntent=new Intent(this,MainActivity.class);
PendingIntent notifyPendingIntent=PendingIntent.getActivity(this,
NOTIFICATION_ID,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notifyBuilder=
new NotificationCompat.Builder(this,PRIMARY_CHANNEL_ID)
.setContentTitle("notify~~~")
.setContentText("uuko notify!!")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentIntent(notifyPendingIntent)
.setAutoCancel(true);
return notifyBuilder;
}
public class NotificationReciever extends BroadcastReceiver{
public NotificationReciever(){
}
@Override
public void onReceive(Context context, Intent intent) {
updateNotification();
}
}
}