public class MainActivity extends AppCompatActivity {
private NotificationManager m_NotificationManager;
private PendingIntent m_PendingIntent;
private int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_NotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
findViewById(R.id.show).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i++;
showNotification();
}
});
findViewById(R.id.cancle).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
m_NotificationManager.cancelAll();
}
});
}
/**
* 设置
*/
private void showNotification() {
//根据服务器传过来的数据不同类型就开启不同的activity
Intent intent = new Intent(this, SecondActivity.class);
intent.addCategory(WINDOW_SERVICE);
m_PendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); //如果转移内容则用m_Intent();
Notification notification = new Notification.Builder(this)
.setAutoCancel(true)
.setContentTitle("这是测试")
.setContentText("这是测试的数据内容adhiosaslkadl;sajdlsajdlskadlsandlksadsadsada")
.setContentIntent(m_PendingIntent)
.setSmallIcon(R.mipmap.back_arrow)
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_SOUND)//声音
.setTicker("测试通知来啦")
.setPriority(Notification.PRIORITY_DEFAULT)
.setOngoing(false)
.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
// Notification.FLAG_SHOW_LIGHTS //三色灯提醒,在使用三色灯提醒时候必须加该标志符
// Notification.FLAG_ONGOING_EVENT //发起正在运行事件(活动中) 等价于setOngoing(true)
// Notification.FLAG_INSISTENT //让声音、振动无限循环,直到用户响应 (取消或者打开)
// Notification.FLAG_ONLY_ALERT_ONCE //发起Notification后,铃声和震动均只执行一次
// Notification.FLAG_AUTO_CANCEL //用户单击通知后自动消失 等价于setAutoCancel(true)
// Notification.FLAG_NO_CLEAR //只有全部清除时,Notification才会清除 ,不清楚该通知(QQ的通知无法清除,就是用的这个)
// Notification.FLAG_FOREGROUND_SERVICE //表示正在运行的服务
notification.vibrate = new long[]{0, 300, 500, 700};
m_NotificationManager.notify(i, notification);//id 不一样才可以提示多条
}
原生Notification
最新推荐文章于 2023-08-02 21:21:48 发布