Android 基础开发组件Notification

1.获取NotificationManager对象

manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

2. 安卓8.0以上版本的通知需要创建NotificationChannel

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
  NotificationChannel channel = new NotificationChannel("june","通知",NotificationManager.IMPORTANCE_HIGH);
  manager.createNotificationChannel(channel);
}

3.使用Builder构造器创建Notification对象,通知设置

setContentTitle

设置通知标题

setContentText

设置通知内容

setSmallIcon

设置通知小图标

setLargeIcon

设置通知大图标

setColor

设置小图标颜色

setContentIntent

设置点击后跳转意图

setAutoCancel

点击通知后自动清除通知

notification = new NotificationCompat.Builder(this,"june")
  .setContentTitle("官方通知")
  .setContentText("相由心生,道法自然")
  .setSmallIcon(R.drawable.ic_android_black_24dp)
  .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.user))
  .setColor(Color.parseColor("#ff0000"))
  .setContentIntent(pendingIntent)
  .setAutoCancel(true)
  .build();

4.设置跳转意图

Intent intent = new Intent(this, NotificationActivity.class);
@SuppressLint("UnspecifiedImmutableFlag") PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

NotificationActivity.java

package com.weijun901.app;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.Nullable;

public class NotificationActivity extends Activity {
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.e("june", "onCreate: 进入NotificationActivity");
  }
}

notification.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".Notification">

  <LinearLayout
    android:orientation="vertical"
    android:layout_width="409dp"
    android:layout_height="729dp" tools:layout_editor_absoluteY="1dp" tools:layout_editor_absoluteX="1dp">
    <Button
      android:text="发送通知"
      android:onClick="send"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" android:id="@+id/button"/>
    <Button
      android:text="取消通知"
      android:onClick="cancel"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" android:id="@+id/button2"/>
  </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Notification.java

package com.weijun901.app;

import android.annotation.SuppressLint;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.app.NotificationCompat;

public class Notification extends AppCompatActivity {

  private NotificationManager manager;
  private android.app.Notification notification;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notification);

    //1.获取NotificationManager对象
    manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    //2.安卓8.0以上版本的通知需要创建NotificationChannel
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
      NotificationChannel channel = new NotificationChannel("june","通知",NotificationManager.IMPORTANCE_HIGH);
      manager.createNotificationChannel(channel);
    }

    //4.设置跳转意图
    Intent intent = new Intent(this, NotificationActivity.class);
    @SuppressLint("UnspecifiedImmutableFlag") PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    //3.使用Builder构造器创建Notification对象,通知设置
    notification = new NotificationCompat.Builder(this,"june")
      .setContentTitle("官方通知")
      .setContentText("相由心生,道法自然")
      .setSmallIcon(R.drawable.ic_android_black_24dp)
      .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.user))
      .setColor(Color.parseColor("#ff0000"))
      .setContentIntent(pendingIntent)
      .setAutoCancel(true)
      .build();
  }

  //5.发送通知
  public void send(View view) {
    manager.notify(1,notification);
  }

  //6.取消通知
  public void cancel(View view) {
    manager.cancel(1);
  }
}

5.发送通知

public void send(View view) {
  manager.notify(1,notification);
}

6.取消通知

public void send(View view) {
  manager.notify(1,notification);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天吼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值