系统通知的简单使用

2020-07-13  本文已影响0人  Time_x
     private void SendMessage() {
    manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    String id= "123";
    CharSequence name= "冒险了";
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_DEFAULT);
        manager.createNotificationChannel(channel);
    }
    Intent intent = new Intent(HomeActivity.this,WebViewActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new NotificationCompat.Builder(this, id)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("提醒一下")
            .setContentText("很危险的")
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .build();

    manager.notify(1,notification);
}

把此方法代码放到你需要的功能下点击即可,当然,消息通知内容自己改成需要的就好.

上一篇 下一篇

猜你喜欢

热点阅读