本代码实现了提示消息的功能:其中Android系统在 8.0 以后增加了通知通道,要正确的在 8.0 系统上使用通知,需要进行版本判定, 然后进行适配,创建出 Builder以后,其他操作不变。如果不对其进行处理的话会出现:Failed to post notification on channel "null"的错误提示。
代码未实现点击信息框后的响应,可以使用PendingIntent来实现(见《第一行代码》p287)。
package com.example.qing.secondd;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
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;
import android.widget.Toast;
public class NotificationActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
Button sendNotice=(Button)findViewById(R.id.send_notice);
sendNotice.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.send_notice:
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
String channelId = "notification_simple";
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(channelId, "simple", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
Notification notification = new NotificationCompat.Builder(NotificationActivity.this, channelId)
.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);
}
else{
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(NotificationActivity.this, channelId)
.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);
Toast.makeText(NotificationActivity.this,"lest 26",Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
}
下方是选自大佬的博客:
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
...
Notification.Builder builder;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String channelId = "notification_simple";
NotificationChannel channel = new NotificationChannel(channelId, "simple", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
builder = new Notification.Builder(this, channelId);
} else {
builder = new Notification.Builder(this);
}
Intent intent = new Intent(this, SimpleCardViewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
// General settings
builder.setContentTitle("simple notification");
builder.setContentText("notification text");
builder.setContentIntent(pendingIntent);
builder.setTicker("");//首次收到的时候,在状态栏中,图标的右侧显示的文字
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setAutoCancel(true);
.setDefaults(Notification.DEFAULT_ALL);//打开呼吸灯,声音,震动,触发系统默认行为
/*Notification.DEFAULT_VIBRATE //添加默认震动提醒 需要VIBRATE permission
Notification.DEFAULT_SOUND //添加默认声音提醒
Notification.DEFAULT_LIGHTS//添加默认三色灯提醒
Notification.DEFAULT_ALL//添加默认以上3种全部提醒*/
//.setLights(Color.YELLOW, 300, 0)//单独设置呼吸灯,一般三种颜色:红,绿,蓝,经测试,小米支持黄色
//.setSound(url)//单独设置声音
//.setVibrate(new long[] { 100, 250, 100, 250, 100, 250 })//单独设置震动
//比较手机sdk版本与Android 5.0 Lollipop的sdk
if(android.os.Build.VERSION.SDK_INT>= android.os.Build.VERSION_CODES.LOLLIPOP) {
/*android5.0加入了一种新的模式Notification的显示等级,共有三种:
VISIBILITY_PUBLIC只有在没有锁屏时会显示通知
VISIBILITY_PRIVATE任何情况都会显示通知
VISIBILITY_SECRET在安全锁和没有锁屏的情况下显示通知*/
builder
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setPriority(Notification.PRIORITY_DEFAULT)//设置该通知优先级
.setCategory(Notification.CATEGORY_MESSAGE)//设置通知类别
//.setColor(context.getResources().getColor(R.color.small_icon_bg_color))//设置smallIcon的背景色
.setFullScreenIntent(contentIntent, true)//将Notification变为悬挂式Notification,使用这种模式的时候,activity 必须处于全屏状态,否则无效
}
// Show notification
manager.notify(0, builder.build());
如果使用呼吸灯或者是震动,需要加上权限
<!-- 闪光灯权限 -->
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<!-- 振动器权限 -->
<uses-permission android:name="android.permission.VIBRATE"/>
---------------------
作者:红发-SHANKS
来源:CSDN
原文:https://blog.csdn.net/xiao6gui/article/details/80895527?utm_source=copy
版权声明:本文为博主原创文章,转载请附上博文链接!